home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Math / MatrixReal.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  87.0 KB  |  3,179 lines

  1.  
  2.  
  3. package Math::MatrixReal;
  4.  
  5. use strict;
  6. use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
  7.  
  8. require Exporter;
  9.  
  10. @ISA = qw(Exporter);
  11.  
  12. @EXPORT = qw();
  13.  
  14. @EXPORT_OK = qw(min max);
  15.  
  16. %EXPORT_TAGS = (all => [@EXPORT_OK]);
  17.  
  18. $VERSION = '1.2';
  19.  
  20. use Carp;
  21.  
  22. use overload
  23.      'neg' => '_negate',
  24.        '~' => '_transpose',
  25.     'bool' => '_boolean',
  26.        '!' => '_not_boolean',
  27.       '""' => '_stringify',
  28.      'abs' => '_norm',
  29.        '+' => '_add',
  30.        '-' => '_subtract',
  31.        '*' => '_multiply',
  32.       '+=' => '_assign_add',
  33.       '-=' => '_assign_subtract',
  34.       '*=' => '_assign_multiply',
  35.       '==' => '_equal',
  36.       '!=' => '_not_equal',
  37.        '<' => '_less_than',
  38.       '<=' => '_less_than_or_equal',
  39.        '>' => '_greater_than',
  40.       '>=' => '_greater_than_or_equal',
  41.       'eq' => '_equal',
  42.       'ne' => '_not_equal',
  43.       'lt' => '_less_than',
  44.       'le' => '_less_than_or_equal',
  45.       'gt' => '_greater_than',
  46.       'ge' => '_greater_than_or_equal',
  47.        '=' => '_clone',
  48. 'fallback' =>   undef;
  49.  
  50. sub new
  51. {
  52.     croak "Usage: \$new_matrix = Math::MatrixReal->new(\$rows,\$columns);"
  53.       if (@_ != 3);
  54.  
  55.     my $proto = shift;
  56.     my $class = ref($proto) || $proto || 'Math::MatrixReal';
  57.     my $rows = shift;
  58.     my $cols = shift;
  59.     my($i,$j);
  60.     my($this);
  61.  
  62.     croak "Math::MatrixReal::new(): number of rows must be > 0"
  63.       if ($rows <= 0);
  64.  
  65.     croak "Math::MatrixReal::new(): number of columns must be > 0"
  66.       if ($cols <= 0);
  67.  
  68.     $this = [ [ ], $rows, $cols ];
  69.     for ( $i = 0; $i < $rows; $i++ )
  70.     {
  71.         $this->[0][$i] = [ ];
  72.         for ( $j = 0; $j < $cols; $j++ )
  73.         {
  74.             $this->[0][$i][$j] = 0;
  75.         }
  76.     }
  77.     bless($this, $class);
  78.     return($this);
  79. }
  80.  
  81. sub new_from_string
  82. {
  83.     croak "Usage: \$new_matrix = Math::MatrixReal->new_from_string(\$string);"
  84.       if (@_ != 2);
  85.  
  86.     my $proto  = shift;
  87.     my $class  = ref($proto) || $proto || 'Math::MatrixReal';
  88.     my $string = shift;
  89.     my($line,$values);
  90.     my($rows,$cols);
  91.     my($row,$col);
  92.     my($warn);
  93.     my($this);
  94.  
  95.     $warn = 0;
  96.     $rows = 0;
  97.     $cols = 0;
  98.     $values = [ ];
  99.     while ($string =~ m!^\s*
  100.   \[ \s+ ( (?: [+-]? \d+ (?: \. \d* )? (?: E [+-]? \d+ )? \s+ )+ ) \] \s*? \n
  101.     !x)
  102.     {
  103.         $line = $1;
  104.         $string = $';
  105.         $values->[$rows] = [ ];
  106.         @{$values->[$rows]} = split(' ', $line);
  107.         $col = @{$values->[$rows]};
  108.         if ($col != $cols)
  109.         {
  110.             unless ($cols == 0) { $warn = 1; }
  111.             if ($col > $cols) { $cols = $col; }
  112.         }
  113.         $rows++;
  114.     }
  115.     if ($string !~ m!^\s*$!)
  116.     {
  117.         croak "Math::MatrixReal::new_from_string(): syntax error in input string";
  118.     }
  119.     if ($rows == 0)
  120.     {
  121.         croak "Math::MatrixReal::new_from_string(): empty input string";
  122.     }
  123.     if ($warn)
  124.     {
  125.         warn "Math::MatrixReal::new_from_string(): missing elements will be set to zero!\n";
  126.     }
  127.     $this = Math::MatrixReal::new($class,$rows,$cols);
  128.     for ( $row = 0; $row < $rows; $row++ )
  129.     {
  130.         for ( $col = 0; $col < @{$values->[$row]}; $col++ )
  131.         {
  132.             $this->[0][$row][$col] = $values->[$row][$col];
  133.         }
  134.     }
  135.     return($this);
  136. }
  137.  
  138. sub shadow
  139. {
  140.     croak "Usage: \$new_matrix = \$some_matrix->shadow();"
  141.       if (@_ != 1);
  142.  
  143.     my($matrix) = @_;
  144.     my($temp);
  145.  
  146.     $temp = $matrix->new($matrix->[1],$matrix->[2]);
  147.     return($temp);
  148. }
  149.  
  150. sub copy
  151. {
  152.     croak "Usage: \$matrix1->copy(\$matrix2);"
  153.       if (@_ != 2);
  154.  
  155.     my($matrix1,$matrix2) = @_;
  156.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  157.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  158.     my($i,$j);
  159.  
  160.     croak "Math::MatrixReal::copy(): matrix size mismatch"
  161.       unless (($rows1 == $rows2) && ($cols1 == $cols2));
  162.  
  163.     for ( $i = 0; $i < $rows1; $i++ )
  164.     {
  165.         for ( $j = 0; $j < $cols1; $j++ )
  166.         {
  167.             $matrix1->[0][$i][$j] = $matrix2->[0][$i][$j];
  168.         }
  169.     }
  170.     if (defined $matrix2->[3]) # is an LR decomposition matrix!
  171.     {
  172.         $matrix1->[3] = $matrix2->[3]; # $sign
  173.         $matrix1->[4] = $matrix2->[4]; # $perm_row
  174.         $matrix1->[5] = $matrix2->[5]; # $perm_col
  175.     }
  176. }
  177.  
  178. sub clone
  179. {
  180.     croak "Usage: \$twin_matrix = \$some_matrix->clone();"
  181.       if (@_ != 1);
  182.  
  183.     my($matrix) = @_;
  184.     my($temp);
  185.  
  186.     $temp = $matrix->new($matrix->[1],$matrix->[2]);
  187.     $temp->copy($matrix);
  188.     return($temp);
  189. }
  190.  
  191. sub row
  192. {
  193.     croak "Usage: \$row_vector = \$matrix->row(\$row);"
  194.       if (@_ != 2);
  195.  
  196.     my($matrix,$row) = @_;
  197.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  198.     my($temp);
  199.     my($j);
  200.  
  201.     croak "Math::MatrixReal::row(): row index out of range"
  202.       if (($row < 1) || ($row > $rows));
  203.  
  204.     $row--;
  205.     $temp = $matrix->new(1,$cols);
  206.     for ( $j = 0; $j < $cols; $j++ )
  207.     {
  208.         $temp->[0][0][$j] = $matrix->[0][$row][$j];
  209.     }
  210.     return($temp);
  211. }
  212.  
  213. sub column
  214. {
  215.     croak "Usage: \$column_vector = \$matrix->column(\$column);"
  216.       if (@_ != 2);
  217.  
  218.     my($matrix,$col) = @_;
  219.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  220.     my($temp);
  221.     my($i);
  222.  
  223.     croak "Math::MatrixReal::column(): column index out of range"
  224.       if (($col < 1) || ($col > $cols));
  225.  
  226.     $col--;
  227.     $temp = $matrix->new($rows,1);
  228.     for ( $i = 0; $i < $rows; $i++ )
  229.     {
  230.         $temp->[0][$i][0] = $matrix->[0][$i][$col];
  231.     }
  232.     return($temp);
  233. }
  234.  
  235. sub _undo_LR
  236. {
  237.     croak "Usage: \$matrix->_undo_LR();"
  238.       if (@_ != 1);
  239.  
  240.     my($this) = @_;
  241.  
  242.     undef $this->[3];
  243.     undef $this->[4];
  244.     undef $this->[5];
  245. }
  246.  
  247. sub zero
  248. {
  249.     croak "Usage: \$matrix->zero();"
  250.       if (@_ != 1);
  251.  
  252.     my($this) = @_;
  253.     my($rows,$cols) = ($this->[1],$this->[2]);
  254.     my($i,$j);
  255.  
  256.     $this->_undo_LR();
  257.  
  258.     for ( $i = 0; $i < $rows; $i++ )
  259.     {
  260.         for ( $j = 0; $j < $cols; $j++ )
  261.         {
  262.             $this->[0][$i][$j] = 0;
  263.         }
  264.     }
  265. }
  266.  
  267. sub one
  268. {
  269.     croak "Usage: \$matrix->one();"
  270.       if (@_ != 1);
  271.  
  272.     my($this) = @_;
  273.     my($rows,$cols) = ($this->[1],$this->[2]);
  274.     my($i,$j);
  275.  
  276.     $this->_undo_LR();
  277.  
  278.     for ( $i = 0; $i < $rows; $i++ )
  279.     {
  280.         for ( $j = 0; $j < $cols; $j++ )
  281.         {
  282.             $this->[0][$i][$j] = 0;
  283.         }
  284.         $this->[0][$i][$i] = 1;
  285.     }
  286. }
  287.  
  288. sub assign
  289. {
  290.     croak "Usage: \$matrix->assign(\$row,\$column,\$value);"
  291.       if (@_ != 4);
  292.  
  293.     my($this,$row,$col,$value) = @_;
  294.     my($rows,$cols) = ($this->[1],$this->[2]);
  295.  
  296.     croak "Math::MatrixReal::assign(): row index out of range"
  297.       if (($row < 1) || ($row > $rows));
  298.  
  299.     croak "Math::MatrixReal::assign(): column index out of range"
  300.       if (($col < 1) || ($col > $cols));
  301.  
  302.     $this->_undo_LR();
  303.  
  304.     $this->[0][--$row][--$col] = $value;
  305. }
  306.  
  307. sub element
  308. {
  309.     croak "Usage: \$value = \$matrix->element(\$row,\$column);"
  310.       if (@_ != 3);
  311.  
  312.     my($this,$row,$col) = @_;
  313.     my($rows,$cols) = ($this->[1],$this->[2]);
  314.  
  315.     croak "Math::MatrixReal::element(): row index out of range"
  316.       if (($row < 1) || ($row > $rows));
  317.  
  318.     croak "Math::MatrixReal::element(): column index out of range"
  319.       if (($col < 1) || ($col > $cols));
  320.  
  321.     return( $this->[0][--$row][--$col] );
  322. }
  323.  
  324. sub dim  #  returns dimensions of a matrix
  325. {
  326.     croak "Usage: (\$rows,\$columns) = \$matrix->dim();"
  327.       if (@_ != 1);
  328.  
  329.     my($matrix) = @_;
  330.  
  331.     return( $matrix->[1], $matrix->[2] );
  332. }
  333.  
  334. sub norm_one  #  maximum of sums of each column
  335. {
  336.     croak "Usage: \$norm_one = \$matrix->norm_one();"
  337.       if (@_ != 1);
  338.  
  339.     my($this) = @_;
  340.     my($rows,$cols) = ($this->[1],$this->[2]);
  341.     my($max,$sum,$i,$j);
  342.  
  343.     $max = 0;
  344.     for ( $j = 0; $j < $cols; $j++ )
  345.     {
  346.         $sum = 0;
  347.         for ( $i = 0; $i < $rows; $i++ )
  348.         {
  349.             $sum += abs( $this->[0][$i][$j] );
  350.         }
  351.         if ($sum > $max) { $max = $sum; }
  352.     }
  353.     return($max);
  354. }
  355.  
  356. sub norm_max  #  maximum of sums of each row
  357. {
  358.     croak "Usage: \$norm_max = \$matrix->norm_max();"
  359.       if (@_ != 1);
  360.  
  361.     my($this) = @_;
  362.     my($rows,$cols) = ($this->[1],$this->[2]);
  363.     my($max,$sum,$i,$j);
  364.  
  365.     $max = 0;
  366.     for ( $i = 0; $i < $rows; $i++ )
  367.     {
  368.         $sum = 0;
  369.         for ( $j = 0; $j < $cols; $j++ )
  370.         {
  371.             $sum += abs( $this->[0][$i][$j] );
  372.         }
  373.         if ($sum > $max) { $max = $sum; }
  374.     }
  375.     return($max);
  376. }
  377.  
  378. sub negate
  379. {
  380.     croak "Usage: \$matrix1->negate(\$matrix2);"
  381.       if (@_ != 2);
  382.  
  383.     my($matrix1,$matrix2) = @_;
  384.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  385.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  386.     my($i,$j);
  387.  
  388.     croak "Math::MatrixReal::negate(): matrix size mismatch"
  389.       unless (($rows1 == $rows2) && ($cols1 == $cols2));
  390.  
  391.     $matrix1->_undo_LR();
  392.  
  393.     for ( $i = 0; $i < $rows1; $i++ )
  394.     {
  395.         for ( $j = 0; $j < $cols1; $j++ )
  396.         {
  397.             $matrix1->[0][$i][$j] = -($matrix2->[0][$i][$j]);
  398.         }
  399.     }
  400. }
  401.  
  402. sub transpose
  403. {
  404.     croak "Usage: \$matrix1->transpose(\$matrix2);"
  405.       if (@_ != 2);
  406.  
  407.     my($matrix1,$matrix2) = @_;
  408.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  409.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  410.     my($i,$j,$swap);
  411.  
  412.     croak "Math::MatrixReal::transpose(): matrix size mismatch"
  413.       unless (($rows1 == $cols2) && ($cols1 == $rows2));
  414.  
  415.     $matrix1->_undo_LR();
  416.  
  417.     if ($rows1 == $cols1)
  418.     {
  419.  
  420.         for ( $i = 0; $i < $rows1; $i++ )
  421.         {
  422.             for ( $j = ($i + 1); $j < $cols1; $j++ )
  423.             {
  424.                 $swap                 = $matrix2->[0][$i][$j];
  425.                 $matrix1->[0][$i][$j] = $matrix2->[0][$j][$i];
  426.                 $matrix1->[0][$j][$i] = $swap;
  427.             }
  428.             $matrix1->[0][$i][$i] = $matrix2->[0][$i][$i];
  429.         }
  430.     }
  431.     else # ($rows1 != $cols1)
  432.     {
  433.         for ( $i = 0; $i < $rows1; $i++ )
  434.         {
  435.             for ( $j = 0; $j < $cols1; $j++ )
  436.             {
  437.                 $matrix1->[0][$i][$j] = $matrix2->[0][$j][$i];
  438.             }
  439.         }
  440.     }
  441. }
  442.  
  443. sub add
  444. {
  445.     croak "Usage: \$matrix1->add(\$matrix2,\$matrix3);"
  446.       if (@_ != 3);
  447.  
  448.     my($matrix1,$matrix2,$matrix3) = @_;
  449.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  450.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  451.     my($rows3,$cols3) = ($matrix3->[1],$matrix3->[2]);
  452.     my($i,$j);
  453.  
  454.     croak "Math::MatrixReal::add(): matrix size mismatch"
  455.       unless (($rows1 == $rows2) && ($rows1 == $rows3) &&
  456.               ($cols1 == $cols2) && ($cols1 == $cols3));
  457.  
  458.     $matrix1->_undo_LR();
  459.  
  460.     for ( $i = 0; $i < $rows1; $i++ )
  461.     {
  462.         for ( $j = 0; $j < $cols1; $j++ )
  463.         {
  464.             $matrix1->[0][$i][$j] =
  465.             $matrix2->[0][$i][$j] + $matrix3->[0][$i][$j];
  466.         }
  467.     }
  468. }
  469.  
  470. sub subtract
  471. {
  472.     croak "Usage: \$matrix1->subtract(\$matrix2,\$matrix3);"
  473.       if (@_ != 3);
  474.  
  475.     my($matrix1,$matrix2,$matrix3) = @_;
  476.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  477.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  478.     my($rows3,$cols3) = ($matrix3->[1],$matrix3->[2]);
  479.     my($i,$j);
  480.  
  481.     croak "Math::MatrixReal::subtract(): matrix size mismatch"
  482.       unless (($rows1 == $rows2) && ($rows1 == $rows3) &&
  483.               ($cols1 == $cols2) && ($cols1 == $cols3));
  484.  
  485.     $matrix1->_undo_LR();
  486.  
  487.     for ( $i = 0; $i < $rows1; $i++ )
  488.     {
  489.         for ( $j = 0; $j < $cols1; $j++ )
  490.         {
  491.             $matrix1->[0][$i][$j] =
  492.             $matrix2->[0][$i][$j] - $matrix3->[0][$i][$j];
  493.         }
  494.     }
  495. }
  496.  
  497. sub multiply_scalar
  498. {
  499.     croak "Usage: \$matrix1->multiply_scalar(\$matrix2,\$scalar);"
  500.       if (@_ != 3);
  501.  
  502.     my($matrix1,$matrix2,$scalar) = @_;
  503.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  504.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  505.     my($i,$j);
  506.  
  507.     croak "Math::MatrixReal::multiply_scalar(): matrix size mismatch"
  508.       unless (($rows1 == $rows2) && ($cols1 == $cols2));
  509.  
  510.     $matrix1->_undo_LR();
  511.  
  512.     for ( $i = 0; $i < $rows1; $i++ )
  513.     {
  514.         for ( $j = 0; $j < $cols1; $j++ )
  515.         {
  516.             $matrix1->[0][$i][$j] = $matrix2->[0][$i][$j] * $scalar;
  517.         }
  518.     }
  519. }
  520.  
  521. sub multiply
  522. {
  523.     croak "Usage: \$product_matrix = \$matrix1->multiply(\$matrix2);"
  524.       if (@_ != 2);
  525.  
  526.     my($matrix1,$matrix2) = @_;
  527.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  528.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  529.     my($i,$j,$k,$sum);
  530.     my($temp);
  531.  
  532.     croak "Math::MatrixReal::multiply(): matrix size mismatch"
  533.       unless ($cols1 == $rows2);
  534.  
  535.     $temp = $matrix1->new($rows1,$cols2);
  536.     for ( $i = 0; $i < $rows1; $i++ )
  537.     {
  538.         for ( $j = 0; $j < $cols2; $j++ )
  539.         {
  540.             $sum = 0;
  541.             for ( $k = 0; $k < $cols1; $k++ )
  542.             {
  543.                 $sum += ( $matrix1->[0][$i][$k] * $matrix2->[0][$k][$j] );
  544.             }
  545.             $temp->[0][$i][$j] = $sum;
  546.         }
  547.     }
  548.     return($temp);
  549. }
  550.  
  551. sub min
  552. {
  553.     croak "Usage: \$minimum = Math::MatrixReal::min(\$number1,\$number2);"
  554.       if (@_ != 2);
  555.  
  556.     return( $_[0] < $_[1] ? $_[0] : $_[1] );
  557. }
  558.  
  559. sub max
  560. {
  561.     croak "Usage: \$maximum = Math::MatrixReal::max(\$number1,\$number2);"
  562.       if (@_ != 2);
  563.  
  564.     return( $_[0] > $_[1] ? $_[0] : $_[1] );
  565. }
  566.  
  567. sub kleene
  568. {
  569.     croak "Usage: \$minimal_cost_matrix = \$cost_matrix->kleene();"
  570.       if (@_ != 1);
  571.  
  572.     my($matrix) = @_;
  573.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  574.     my($i,$j,$k,$n);
  575.     my($temp);
  576.  
  577.     croak "Math::MatrixReal::kleene(): matrix is not quadratic"
  578.       unless ($rows == $cols);
  579.  
  580.     $temp = $matrix->new($rows,$cols);
  581.     $temp->copy($matrix);
  582.     $temp->_undo_LR();
  583.     $n = $rows;
  584.     for ( $i = 0; $i < $n; $i++ )
  585.     {
  586.         $temp->[0][$i][$i] = min( $temp->[0][$i][$i] , 0 );
  587.     }
  588.     for ( $k = 0; $k < $n; $k++ )
  589.     {
  590.         for ( $i = 0; $i < $n; $i++ )
  591.         {
  592.             for ( $j = 0; $j < $n; $j++ )
  593.             {
  594.                 $temp->[0][$i][$j] = min( $temp->[0][$i][$j] ,
  595.                                         ( $temp->[0][$i][$k] +
  596.                                           $temp->[0][$k][$j] ) );
  597.             }
  598.         }
  599.     }
  600.     return($temp);
  601. }
  602.  
  603. sub normalize
  604. {
  605.     croak "Usage: (\$norm_matrix,\$norm_vector) = \$matrix->normalize(\$vector);"
  606.       if (@_ != 2);
  607.  
  608.     my($matrix,$vector) = @_;
  609.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  610.     my($norm_matrix,$norm_vector);
  611.     my($max,$val);
  612.     my($i,$j,$n);
  613.  
  614.     croak "Math::MatrixReal::normalize(): matrix is not quadratic"
  615.       unless ($rows == $cols);
  616.  
  617.     $n = $rows;
  618.  
  619.     croak "Math::MatrixReal::normalize(): vector is not a column vector"
  620.       unless ($vector->[2] == 1);
  621.  
  622.     croak "Math::MatrixReal::normalize(): matrix and vector size mismatch"
  623.       unless ($vector->[1] == $n);
  624.  
  625.     $norm_matrix = $matrix->new($n,$n);
  626.     $norm_vector = $vector->new($n,1);
  627.  
  628.     $norm_matrix->copy($matrix);
  629.     $norm_vector->copy($vector);
  630.  
  631.     $norm_matrix->_undo_LR();
  632.  
  633.     for ( $i = 0; $i < $n; $i++ )
  634.     {
  635.         $max = abs($norm_vector->[0][$i][0]);
  636.         for ( $j = 0; $j < $n; $j++ )
  637.         {
  638.             $val = abs($norm_matrix->[0][$i][$j]);
  639.             if ($val > $max) { $max = $val; }
  640.         }
  641.         if ($max != 0)
  642.         {
  643.             $norm_vector->[0][$i][0] /= $max;
  644.             for ( $j = 0; $j < $n; $j++ )
  645.             {
  646.                 $norm_matrix->[0][$i][$j] /= $max;
  647.             }
  648.         }
  649.     }
  650.     return($norm_matrix,$norm_vector);
  651. }
  652.  
  653. sub decompose_LR
  654. {
  655.     croak "Usage: \$LR_matrix = \$matrix->decompose_LR();"
  656.       if (@_ != 1);
  657.  
  658.     my($matrix) = @_;
  659.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  660.     my($perm_row,$perm_col);
  661.     my($row,$col,$max);
  662.     my($i,$j,$k,$n);
  663.     my($sign) = 1;
  664.     my($swap);
  665.     my($temp);
  666.  
  667.     croak "Math::MatrixReal::decompose_LR(): matrix is not quadratic"
  668.       unless ($rows == $cols);
  669.  
  670.     $temp = $matrix->new($rows,$cols);
  671.     $temp->copy($matrix);
  672.     $n = $rows;
  673.     $perm_row = [ ];
  674.     $perm_col = [ ];
  675.     for ( $i = 0; $i < $n; $i++ )
  676.     {
  677.         $perm_row->[$i] = $i;
  678.         $perm_col->[$i] = $i;
  679.     }
  680.     NONZERO:
  681.     for ( $k = 0; $k < $n; $k++ ) # use Gauss's algorithm:
  682.     {
  683.  
  684.         $max = 0;
  685.         for ( $i = $k; $i < $n; $i++ )
  686.         {
  687.             for ( $j = $k; $j < $n; $j++ )
  688.             {
  689.                 if (($swap = abs($temp->[0][$i][$j])) > $max)
  690.                 {
  691.                     $max = $swap;
  692.                     $row = $i;
  693.                     $col = $j;
  694.                 }
  695.             }
  696.         }
  697.         last NONZERO if ($max == 0); # (all remaining elements are zero)
  698.         if ($k != $row) # swap row $k and row $row:
  699.         {
  700.             $sign = -$sign;
  701.             $swap             = $perm_row->[$k];
  702.             $perm_row->[$k]   = $perm_row->[$row];
  703.             $perm_row->[$row] = $swap;
  704.             for ( $j = 0; $j < $n; $j++ )
  705.             {
  706.  
  707.                 $swap                = $temp->[0][$k][$j];
  708.                 $temp->[0][$k][$j]   = $temp->[0][$row][$j];
  709.                 $temp->[0][$row][$j] = $swap;
  710.             }
  711.         }
  712.         if ($k != $col) # swap column $k and column $col:
  713.         {
  714.             $sign = -$sign;
  715.             $swap             = $perm_col->[$k];
  716.             $perm_col->[$k]   = $perm_col->[$col];
  717.             $perm_col->[$col] = $swap;
  718.             for ( $i = 0; $i < $n; $i++ )
  719.             {
  720.                 $swap                = $temp->[0][$i][$k];
  721.                 $temp->[0][$i][$k]   = $temp->[0][$i][$col];
  722.                 $temp->[0][$i][$col] = $swap;
  723.             }
  724.         }
  725.         for ( $i = ($k + 1); $i < $n; $i++ )
  726.         {
  727.  
  728.             $swap = $temp->[0][$i][$k] / $temp->[0][$k][$k];
  729.             if ($swap != 0)
  730.             {
  731.  
  732.                 for ( $j = ($k + 1); $j < $n; $j++ )
  733.                 {
  734.                     $temp->[0][$i][$j] -= $temp->[0][$k][$j] * $swap;
  735.                 }
  736.  
  737.  
  738.                 $temp->[0][$i][$k] = $swap;
  739.             }
  740.         }
  741.     }
  742.     $temp->[3] = $sign;
  743.     $temp->[4] = $perm_row;
  744.     $temp->[5] = $perm_col;
  745.     return($temp);
  746. }
  747.  
  748. sub solve_LR
  749. {
  750.     croak "Usage: (\$dimension,\$x_vector,\$base_matrix) = \$LR_matrix->solve_LR(\$b_vector);"
  751.       if (@_ != 2);
  752.  
  753.     my($LR_matrix,$b_vector) = @_;
  754.     my($rows,$cols) = ($LR_matrix->[1],$LR_matrix->[2]);
  755.     my($dimension,$x_vector,$base_matrix);
  756.     my($perm_row,$perm_col);
  757.     my($y_vector,$sum);
  758.     my($i,$j,$k,$n);
  759.  
  760.     croak "Math::MatrixReal::solve_LR(): not an LR decomposition matrix"
  761.       unless ((defined $LR_matrix->[3]) && ($rows == $cols));
  762.  
  763.     $n = $rows;
  764.  
  765.     croak "Math::MatrixReal::solve_LR(): vector is not a column vector"
  766.       unless ($b_vector->[2] == 1);
  767.  
  768.     croak "Math::MatrixReal::solve_LR(): matrix and vector size mismatch"
  769.       unless ($b_vector->[1] == $n);
  770.  
  771.     $perm_row = $LR_matrix->[4];
  772.     $perm_col = $LR_matrix->[5];
  773.  
  774.     $x_vector    =   $b_vector->new($n,1);
  775.     $y_vector    =   $b_vector->new($n,1);
  776.     $base_matrix = $LR_matrix->new($n,$n);
  777.  
  778.  
  779.     for ( $i = 0; $i < $n; $i++ ) # calculate $y_vector:
  780.     {
  781.         $sum = $b_vector->[0][($perm_row->[$i])][0];
  782.         for ( $j = 0; $j < $i; $j++ )
  783.         {
  784.             $sum -= $LR_matrix->[0][$i][$j] * $y_vector->[0][$j][0];
  785.         }
  786.         $y_vector->[0][$i][0] = $sum;
  787.     }
  788.  
  789.     $dimension = 0;
  790.     for ( $i = ($n - 1); $i >= 0; $i-- ) # calculate $x_vector:
  791.     {
  792.         if ($LR_matrix->[0][$i][$i] == 0)
  793.         {
  794.             if ($y_vector->[0][$i][0] != 0)
  795.             {
  796.                 return(); # a solution does not exist!
  797.             }
  798.             else
  799.             {
  800.                 $dimension++;
  801.                 $x_vector->[0][($perm_col->[$i])][0] = 0;
  802.             }
  803.         }
  804.         else
  805.         {
  806.             $sum = $y_vector->[0][$i][0];
  807.             for ( $j = ($i + 1); $j < $n; $j++ )
  808.             {
  809.                 $sum -= $LR_matrix->[0][$i][$j] *
  810.                     $x_vector->[0][($perm_col->[$j])][0];
  811.             }
  812.             $x_vector->[0][($perm_col->[$i])][0] =
  813.                 $sum / $LR_matrix->[0][$i][$i];
  814.         }
  815.     }
  816.     if ($dimension)
  817.     {
  818.         if ($dimension == $n)
  819.         {
  820.             $base_matrix->one();
  821.         }
  822.         else
  823.         {
  824.             for ( $k = 0; $k < $dimension; $k++ )
  825.             {
  826.                 $base_matrix->[0][($perm_col->[($n-$k-1)])][$k] = 1;
  827.                 for ( $i = ($n-$dimension-1); $i >= 0; $i-- )
  828.                 {
  829.                     $sum = 0;
  830.                     for ( $j = ($i + 1); $j < $n; $j++ )
  831.                     {
  832.                         $sum -= $LR_matrix->[0][$i][$j] *
  833.                             $base_matrix->[0][($perm_col->[$j])][$k];
  834.                     }
  835.                     $base_matrix->[0][($perm_col->[$i])][$k] =
  836.                         $sum / $LR_matrix->[0][$i][$i];
  837.                 }
  838.             }
  839.         }
  840.     }
  841.     return( $dimension, $x_vector, $base_matrix );
  842. }
  843.  
  844. sub invert_LR
  845. {
  846.     croak "Usage: \$inverse_matrix = \$LR_matrix->invert_LR();"
  847.       if (@_ != 1);
  848.  
  849.     my($matrix) = @_;
  850.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  851.     my($inv_matrix,$x_vector,$y_vector);
  852.     my($i,$j,$n);
  853.  
  854.     croak "Math::MatrixReal::invert_LR(): not an LR decomposition matrix"
  855.       unless ((defined $matrix->[3]) && ($rows == $cols));
  856.  
  857.     $n = $rows;
  858.     if ($matrix->[0][$n-1][$n-1] != 0)
  859.     {
  860.         $inv_matrix = $matrix->new($n,$n);
  861.         $y_vector   = $matrix->new($n,1);
  862.         for ( $j = 0; $j < $n; $j++ )
  863.         {
  864.             if ($j > 0)
  865.             {
  866.                 $y_vector->[0][$j-1][0] = 0;
  867.             }
  868.             $y_vector->[0][$j][0] = 1;
  869.             if (($rows,$x_vector,$cols) = $matrix->solve_LR($y_vector))
  870.             {
  871.                 for ( $i = 0; $i < $n; $i++ )
  872.                 {
  873.                     $inv_matrix->[0][$i][$j] = $x_vector->[0][$i][0];
  874.                 }
  875.             }
  876.             else
  877.             {
  878.                 die "Math::MatrixReal::invert_LR(): unexpected error - please inform author!\n";
  879.             }
  880.         }
  881.         return($inv_matrix);
  882.     }
  883.     else { return(); } # matrix is not invertible!
  884. }
  885.  
  886. sub condition
  887. {
  888.  
  889.     croak "Usage: \$condition = \$matrix->condition(\$inverse_matrix);"
  890.       if (@_ != 2);
  891.  
  892.     my($matrix1,$matrix2) = @_;
  893.     my($rows1,$cols1) = ($matrix1->[1],$matrix1->[2]);
  894.     my($rows2,$cols2) = ($matrix2->[1],$matrix2->[2]);
  895.  
  896.     croak "Math::MatrixReal::condition(): 1st matrix is not quadratic"
  897.       unless ($rows1 == $cols1);
  898.  
  899.     croak "Math::MatrixReal::condition(): 2nd matrix is not quadratic"
  900.       unless ($rows2 == $cols2);
  901.  
  902.     croak "Math::MatrixReal::condition(): matrix size mismatch"
  903.       unless (($rows1 == $rows2) && ($cols1 == $cols2));
  904.  
  905.     return( $matrix1->norm_one() * $matrix2->norm_one() );
  906. }
  907.  
  908. sub det_LR  #  determinant of LR decomposition matrix
  909. {
  910.     croak "Usage: \$determinant = \$LR_matrix->det_LR();"
  911.       if (@_ != 1);
  912.  
  913.     my($matrix) = @_;
  914.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  915.     my($k,$det);
  916.  
  917.     croak "Math::MatrixReal::det_LR(): not an LR decomposition matrix"
  918.       unless ((defined $matrix->[3]) && ($rows == $cols));
  919.  
  920.     $det = $matrix->[3];
  921.     for ( $k = 0; $k < $rows; $k++ )
  922.     {
  923.         $det *= $matrix->[0][$k][$k];
  924.     }
  925.     return($det);
  926. }
  927.  
  928. sub order_LR  #  order of LR decomposition matrix (number of non-zero equations)
  929. {
  930.     croak "Usage: \$order = \$LR_matrix->order_LR();"
  931.       if (@_ != 1);
  932.  
  933.     my($matrix) = @_;
  934.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  935.     my($order);
  936.  
  937.     croak "Math::MatrixReal::order_LR(): not an LR decomposition matrix"
  938.       unless ((defined $matrix->[3]) && ($rows == $cols));
  939.  
  940.     ZERO:
  941.     for ( $order = ($rows - 1); $order >= 0; $order-- )
  942.     {
  943.         last ZERO if ($matrix->[0][$order][$order] != 0);
  944.     }
  945.     return(++$order);
  946. }
  947.  
  948. sub scalar_product
  949. {
  950.     croak "Usage: \$scalar_product = \$vector1->scalar_product(\$vector2);"
  951.       if (@_ != 2);
  952.  
  953.     my($vector1,$vector2) = @_;
  954.     my($rows1,$cols1) = ($vector1->[1],$vector1->[2]);
  955.     my($rows2,$cols2) = ($vector2->[1],$vector2->[2]);
  956.     my($k,$sum);
  957.  
  958.     croak "Math::MatrixReal::scalar_product(): 1st vector is not a column vector"
  959.       unless ($cols1 == 1);
  960.  
  961.     croak "Math::MatrixReal::scalar_product(): 2nd vector is not a column vector"
  962.       unless ($cols2 == 1);
  963.  
  964.     croak "Math::MatrixReal::scalar_product(): vector size mismatch"
  965.       unless ($rows1 == $rows2);
  966.  
  967.     $sum = 0;
  968.     for ( $k = 0; $k < $rows1; $k++ )
  969.     {
  970.         $sum += $vector1->[0][$k][0] * $vector2->[0][$k][0];
  971.     }
  972.     return($sum);
  973. }
  974.  
  975. sub vector_product
  976. {
  977.     croak "Usage: \$vector_product = \$vector1->vector_product(\$vector2);"
  978.       if (@_ != 2);
  979.  
  980.     my($vector1,$vector2) = @_;
  981.     my($rows1,$cols1) = ($vector1->[1],$vector1->[2]);
  982.     my($rows2,$cols2) = ($vector2->[1],$vector2->[2]);
  983.     my($temp);
  984.     my($n);
  985.  
  986.     croak "Math::MatrixReal::vector_product(): 1st vector is not a column vector"
  987.       unless ($cols1 == 1);
  988.  
  989.     croak "Math::MatrixReal::vector_product(): 2nd vector is not a column vector"
  990.       unless ($cols2 == 1);
  991.  
  992.     croak "Math::MatrixReal::vector_product(): vector size mismatch"
  993.       unless ($rows1 == $rows2);
  994.  
  995.     $n = $rows1;
  996.  
  997.     croak "Math::MatrixReal::vector_product(): only defined for 3 dimensions"
  998.       unless ($n == 3);
  999.  
  1000.     $temp = $vector1->new($n,1);
  1001.     $temp->[0][0][0] = $vector1->[0][1][0] * $vector2->[0][2][0] -
  1002.                        $vector1->[0][2][0] * $vector2->[0][1][0];
  1003.     $temp->[0][1][0] = $vector1->[0][2][0] * $vector2->[0][0][0] -
  1004.                        $vector1->[0][0][0] * $vector2->[0][2][0];
  1005.     $temp->[0][2][0] = $vector1->[0][0][0] * $vector2->[0][1][0] -
  1006.                        $vector1->[0][1][0] * $vector2->[0][0][0];
  1007.     return($temp);
  1008. }
  1009.  
  1010. sub length
  1011. {
  1012.     croak "Usage: \$length = \$vector->length();"
  1013.       if (@_ != 1);
  1014.  
  1015.     my($vector) = @_;
  1016.     my($rows,$cols) = ($vector->[1],$vector->[2]);
  1017.     my($k,$comp,$sum);
  1018.  
  1019.     croak "Math::MatrixReal::length(): vector is not a column vector"
  1020.       unless ($cols == 1);
  1021.  
  1022.     $sum = 0;
  1023.     for ( $k = 0; $k < $rows; $k++ )
  1024.     {
  1025.         $comp = $vector->[0][$k][0];
  1026.         $sum += $comp * $comp;
  1027.     }
  1028.     return( sqrt( $sum ) );
  1029. }
  1030.  
  1031. sub _init_iteration
  1032. {
  1033.     croak "Usage: \$which_norm = \$matrix->_init_iteration();"
  1034.       if (@_ != 1);
  1035.  
  1036.     my($matrix) = @_;
  1037.     my($rows,$cols) = ($matrix->[1],$matrix->[2]);
  1038.     my($ok,$max,$sum,$norm);
  1039.     my($i,$j,$n);
  1040.  
  1041.     croak "Math::MatrixReal::_init_iteration(): matrix is not quadratic"
  1042.       unless ($rows == $cols);
  1043.  
  1044.     $ok = 1;
  1045.     $n = $rows;
  1046.     for ( $i = 0; $i < $n; $i++ )
  1047.     {
  1048.         if ($matrix->[0][$i][$i] == 0) { $ok = 0; }
  1049.     }
  1050.     if ($ok)
  1051.     {
  1052.         $norm = 1; # norm_one
  1053.         $max = 0;
  1054.         for ( $j = 0; $j < $n; $j++ )
  1055.         {
  1056.             $sum = 0;
  1057.             for ( $i = 0; $i < $j; $i++ )
  1058.             {
  1059.                 $sum += abs($matrix->[0][$i][$j]);
  1060.             }
  1061.             for ( $i = ($j + 1); $i < $n; $i++ )
  1062.             {
  1063.                 $sum += abs($matrix->[0][$i][$j]);
  1064.             }
  1065.             $sum /= abs($matrix->[0][$j][$j]);
  1066.             if ($sum > $max) { $max = $sum; }
  1067.         }
  1068.         $ok = ($max < 1);
  1069.         unless ($ok)
  1070.         {
  1071.             $norm = -1; # norm_max
  1072.             $max = 0;
  1073.             for ( $i = 0; $i < $n; $i++ )
  1074.             {
  1075.                 $sum = 0;
  1076.                 for ( $j = 0; $j < $i; $j++ )
  1077.                 {
  1078.                     $sum += abs($matrix->[0][$i][$j]);
  1079.                 }
  1080.                 for ( $j = ($i + 1); $j < $n; $j++ )
  1081.                 {
  1082.                     $sum += abs($matrix->[0][$i][$j]);
  1083.                 }
  1084.                 $sum /= abs($matrix->[0][$i][$i]);
  1085.                 if ($sum > $max) { $max = $sum; }
  1086.             }
  1087.             $ok = ($max < 1)
  1088.         }
  1089.     }
  1090.     if ($ok) { return($norm); }
  1091.     else     { return(0); }
  1092. }
  1093.  
  1094. sub solve_GSM  #  Global Step Method
  1095. {
  1096.     croak "Usage: \$xn_vector = \$matrix->solve_GSM(\$x0_vector,\$b_vector,\$epsilon);"
  1097.       if (@_ != 4);
  1098.  
  1099.     my($matrix,$x0_vector,$b_vector,$epsilon) = @_;
  1100.     my($rows1,$cols1) = (   $matrix->[1],   $matrix->[2]);
  1101.     my($rows2,$cols2) = ($x0_vector->[1],$x0_vector->[2]);
  1102.     my($rows3,$cols3) = ( $b_vector->[1], $b_vector->[2]);
  1103.     my($norm,$sum,$diff);
  1104.     my($xn_vector);
  1105.     my($i,$j,$n);
  1106.  
  1107.     croak "Math::MatrixReal::solve_GSM(): matrix is not quadratic"
  1108.       unless ($rows1 == $cols1);
  1109.  
  1110.     $n = $rows1;
  1111.  
  1112.     croak "Math::MatrixReal::solve_GSM(): 1st vector is not a column vector"
  1113.       unless ($cols2 == 1);
  1114.  
  1115.     croak "Math::MatrixReal::solve_GSM(): 2nd vector is not a column vector"
  1116.       unless ($cols3 == 1);
  1117.  
  1118.     croak "Math::MatrixReal::solve_GSM(): matrix and vector size mismatch"
  1119.       unless (($rows2 == $n) && ($rows3 == $n));
  1120.  
  1121.     return() unless ($norm = $matrix->_init_iteration());
  1122.  
  1123.     $xn_vector = $x0_vector->new($n,1);
  1124.  
  1125.     $diff = $epsilon + 1;
  1126.     while ($diff >= $epsilon)
  1127.     {
  1128.         for ( $i = 0; $i < $n; $i++ )
  1129.         {
  1130.             $sum = $b_vector->[0][$i][0];
  1131.             for ( $j = 0; $j < $i; $j++ )
  1132.             {
  1133.                 $sum -= $matrix->[0][$i][$j] * $x0_vector->[0][$j][0];
  1134.             }
  1135.             for ( $j = ($i + 1); $j < $n; $j++ )
  1136.             {
  1137.                 $sum -= $matrix->[0][$i][$j] * $x0_vector->[0][$j][0];
  1138.             }
  1139.             $xn_vector->[0][$i][0] = $sum / $matrix->[0][$i][$i];
  1140.         }
  1141.         $x0_vector->subtract($x0_vector,$xn_vector);
  1142.         if ($norm > 0) { $diff = $x0_vector->norm_one(); }
  1143.         else           { $diff = $x0_vector->norm_max(); }
  1144.         for ( $i = 0; $i < $n; $i++ )
  1145.         {
  1146.             $x0_vector->[0][$i][0] = $xn_vector->[0][$i][0];
  1147.         }
  1148.     }
  1149.     return($xn_vector);
  1150. }
  1151.  
  1152. sub solve_SSM  #  Single Step Method
  1153. {
  1154.     croak "Usage: \$xn_vector = \$matrix->solve_SSM(\$x0_vector,\$b_vector,\$epsilon);"
  1155.       if (@_ != 4);
  1156.  
  1157.     my($matrix,$x0_vector,$b_vector,$epsilon) = @_;
  1158.     my($rows1,$cols1) = (   $matrix->[1],   $matrix->[2]);
  1159.     my($rows2,$cols2) = ($x0_vector->[1],$x0_vector->[2]);
  1160.     my($rows3,$cols3) = ( $b_vector->[1], $b_vector->[2]);
  1161.     my($norm,$sum,$diff);
  1162.     my($xn_vector);
  1163.     my($i,$j,$n);
  1164.  
  1165.     croak "Math::MatrixReal::solve_SSM(): matrix is not quadratic"
  1166.       unless ($rows1 == $cols1);
  1167.  
  1168.     $n = $rows1;
  1169.  
  1170.     croak "Math::MatrixReal::solve_SSM(): 1st vector is not a column vector"
  1171.       unless ($cols2 == 1);
  1172.  
  1173.     croak "Math::MatrixReal::solve_SSM(): 2nd vector is not a column vector"
  1174.       unless ($cols3 == 1);
  1175.  
  1176.     croak "Math::MatrixReal::solve_SSM(): matrix and vector size mismatch"
  1177.       unless (($rows2 == $n) && ($rows3 == $n));
  1178.  
  1179.     return() unless ($norm = $matrix->_init_iteration());
  1180.  
  1181.     $xn_vector = $x0_vector->new($n,1);
  1182.     $xn_vector->copy($x0_vector);
  1183.  
  1184.     $diff = $epsilon + 1;
  1185.     while ($diff >= $epsilon)
  1186.     {
  1187.         for ( $i = 0; $i < $n; $i++ )
  1188.         {
  1189.             $sum = $b_vector->[0][$i][0];
  1190.             for ( $j = 0; $j < $i; $j++ )
  1191.             {
  1192.                 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
  1193.             }
  1194.             for ( $j = ($i + 1); $j < $n; $j++ )
  1195.             {
  1196.                 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
  1197.             }
  1198.             $xn_vector->[0][$i][0] = $sum / $matrix->[0][$i][$i];
  1199.         }
  1200.         $x0_vector->subtract($x0_vector,$xn_vector);
  1201.         if ($norm > 0) { $diff = $x0_vector->norm_one(); }
  1202.         else           { $diff = $x0_vector->norm_max(); }
  1203.         for ( $i = 0; $i < $n; $i++ )
  1204.         {
  1205.             $x0_vector->[0][$i][0] = $xn_vector->[0][$i][0];
  1206.         }
  1207.     }
  1208.     return($xn_vector);
  1209. }
  1210.  
  1211. sub solve_RM  #  Relaxation Method
  1212. {
  1213.     croak "Usage: \$xn_vector = \$matrix->solve_RM(\$x0_vector,\$b_vector,\$weight,\$epsilon);"
  1214.       if (@_ != 5);
  1215.  
  1216.     my($matrix,$x0_vector,$b_vector,$weight,$epsilon) = @_;
  1217.     my($rows1,$cols1) = (   $matrix->[1],   $matrix->[2]);
  1218.     my($rows2,$cols2) = ($x0_vector->[1],$x0_vector->[2]);
  1219.     my($rows3,$cols3) = ( $b_vector->[1], $b_vector->[2]);
  1220.     my($norm,$sum,$diff);
  1221.     my($xn_vector);
  1222.     my($i,$j,$n);
  1223.  
  1224.     croak "Math::MatrixReal::solve_RM(): matrix is not quadratic"
  1225.       unless ($rows1 == $cols1);
  1226.  
  1227.     $n = $rows1;
  1228.  
  1229.     croak "Math::MatrixReal::solve_RM(): 1st vector is not a column vector"
  1230.       unless ($cols2 == 1);
  1231.  
  1232.     croak "Math::MatrixReal::solve_RM(): 2nd vector is not a column vector"
  1233.       unless ($cols3 == 1);
  1234.  
  1235.     croak "Math::MatrixReal::solve_RM(): matrix and vector size mismatch"
  1236.       unless (($rows2 == $n) && ($rows3 == $n));
  1237.  
  1238.     return() unless ($norm = $matrix->_init_iteration());
  1239.  
  1240.     $xn_vector = $x0_vector->new($n,1);
  1241.     $xn_vector->copy($x0_vector);
  1242.  
  1243.     $diff = $epsilon + 1;
  1244.     while ($diff >= $epsilon)
  1245.     {
  1246.         for ( $i = 0; $i < $n; $i++ )
  1247.         {
  1248.             $sum = $b_vector->[0][$i][0];
  1249.             for ( $j = 0; $j < $i; $j++ )
  1250.             {
  1251.                 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
  1252.             }
  1253.             for ( $j = ($i + 1); $j < $n; $j++ )
  1254.             {
  1255.                 $sum -= $matrix->[0][$i][$j] * $xn_vector->[0][$j][0];
  1256.             }
  1257.             $xn_vector->[0][$i][0] = $weight * ( $sum / $matrix->[0][$i][$i] )
  1258.                                    + (1 - $weight) * $xn_vector->[0][$i][0];
  1259.         }
  1260.         $x0_vector->subtract($x0_vector,$xn_vector);
  1261.         if ($norm > 0) { $diff = $x0_vector->norm_one(); }
  1262.         else           { $diff = $x0_vector->norm_max(); }
  1263.         for ( $i = 0; $i < $n; $i++ )
  1264.         {
  1265.             $x0_vector->[0][$i][0] = $xn_vector->[0][$i][0];
  1266.         }
  1267.     }
  1268.     return($xn_vector);
  1269. }
  1270.  
  1271.  
  1272. sub _negate
  1273. {
  1274.     my($object,$argument,$flag) = @_;
  1275.     my($temp);
  1276.  
  1277.     $temp = $object->new($object->[1],$object->[2]);
  1278.     $temp->negate($object);
  1279.     return($temp);
  1280. }
  1281.  
  1282. sub _transpose
  1283. {
  1284.     my($object,$argument,$flag) = @_;
  1285.     my($temp);
  1286.  
  1287.     $temp = $object->new($object->[2],$object->[1]);
  1288.     $temp->transpose($object);
  1289.     return($temp);
  1290. }
  1291.  
  1292. sub _boolean
  1293. {
  1294.     my($object,$argument,$flag) = @_;
  1295.     my($rows,$cols) = ($object->[1],$object->[2]);
  1296.     my($i,$j,$result);
  1297.  
  1298.     $result = 0;
  1299.     BOOL:
  1300.     for ( $i = 0; $i < $rows; $i++ )
  1301.     {
  1302.         for ( $j = 0; $j < $cols; $j++ )
  1303.         {
  1304.             if ($object->[0][$i][$j] != 0)
  1305.             {
  1306.                 $result = 1;
  1307.                 last BOOL;
  1308.             }
  1309.         }
  1310.     }
  1311.     return($result);
  1312. }
  1313.  
  1314. sub _not_boolean
  1315. {
  1316.     my($object,$argument,$flag) = @_;
  1317.     my($rows,$cols) = ($object->[1],$object->[2]);
  1318.     my($i,$j,$result);
  1319.  
  1320.     $result = 1;
  1321.     NOTBOOL:
  1322.     for ( $i = 0; $i < $rows; $i++ )
  1323.     {
  1324.         for ( $j = 0; $j < $cols; $j++ )
  1325.         {
  1326.             if ($object->[0][$i][$j] != 0)
  1327.             {
  1328.                 $result = 0;
  1329.                 last NOTBOOL;
  1330.             }
  1331.         }
  1332.     }
  1333.     return($result);
  1334. }
  1335.  
  1336. sub _stringify
  1337. {
  1338.     my($object,$argument,$flag) = @_;
  1339.     my($rows,$cols) = ($object->[1],$object->[2]);
  1340.     my($i,$j,$s);
  1341.  
  1342.     $s = '';
  1343.     for ( $i = 0; $i < $rows; $i++ )
  1344.     {
  1345.         $s .= "[ ";
  1346.         for ( $j = 0; $j < $cols; $j++ )
  1347.         {
  1348.             $s .= sprintf("% #-19.12E ", $object->[0][$i][$j]);
  1349.         }
  1350.         $s .= "]\n";
  1351.     }
  1352.     return($s);
  1353. }
  1354.  
  1355. sub _norm
  1356. {
  1357.     my($object,$argument,$flag) = @_;
  1358.  
  1359.     return( $object->norm_one() );
  1360. }
  1361.  
  1362. sub _add
  1363. {
  1364.     my($object,$argument,$flag) = @_;
  1365.     my($name) = "'+'"; #&_trace($name,$object,$argument,$flag);
  1366.     my($temp);
  1367.  
  1368.     if ((defined $argument) && ref($argument) &&
  1369.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1370.     {
  1371.         if (defined $flag)
  1372.         {
  1373.             $temp = $object->new($object->[1],$object->[2]);
  1374.             $temp->add($object,$argument);
  1375.             return($temp);
  1376.         }
  1377.         else
  1378.         {
  1379.             $object->add($object,$argument);
  1380.             return($object);
  1381.         }
  1382.     }
  1383.     else
  1384.     {
  1385.         croak "Math::MatrixReal $name: wrong argument type";
  1386.     }
  1387. }
  1388.  
  1389. sub _subtract
  1390. {
  1391.     my($object,$argument,$flag) = @_;
  1392.     my($name) = "'-'"; #&_trace($name,$object,$argument,$flag);
  1393.     my($temp);
  1394.  
  1395.     if ((defined $argument) && ref($argument) &&
  1396.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1397.     {
  1398.         if (defined $flag)
  1399.         {
  1400.             $temp = $object->new($object->[1],$object->[2]);
  1401.             if ($flag) { $temp->subtract($argument,$object); }
  1402.             else       { $temp->subtract($object,$argument); }
  1403.             return($temp);
  1404.         }
  1405.         else
  1406.         {
  1407.             $object->subtract($object,$argument);
  1408.             return($object);
  1409.         }
  1410.     }
  1411.     else
  1412.     {
  1413.         croak "Math::MatrixReal $name: wrong argument type";
  1414.     }
  1415. }
  1416.  
  1417. sub _multiply
  1418. {
  1419.     my($object,$argument,$flag) = @_;
  1420.     my($name) = "'*'"; #&_trace($name,$object,$argument,$flag);
  1421.     my($temp);
  1422.  
  1423.     if ((defined $argument) && ref($argument) &&
  1424.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1425.     {
  1426.         if ((defined $flag) && $flag)
  1427.         {
  1428.             return( multiply($argument,$object) );
  1429.         }
  1430.         else
  1431.         {
  1432.             return( multiply($object,$argument) );
  1433.         }
  1434.     }
  1435.     elsif ((defined $argument) && !(ref($argument)))
  1436.     {
  1437.         if (defined $flag)
  1438.         {
  1439.             $temp = $object->new($object->[1],$object->[2]);
  1440.             $temp->multiply_scalar($object,$argument);
  1441.             return($temp);
  1442.         }
  1443.         else
  1444.         {
  1445.             $object->multiply_scalar($object,$argument);
  1446.             return($object);
  1447.         }
  1448.     }
  1449.     else
  1450.     {
  1451.         croak "Math::MatrixReal $name: wrong argument type";
  1452.     }
  1453. }
  1454.  
  1455. sub _assign_add
  1456. {
  1457.     my($object,$argument,$flag) = @_;
  1458.  
  1459.     return( &_add($object,$argument,undef) );
  1460. }
  1461.  
  1462. sub _assign_subtract
  1463. {
  1464.     my($object,$argument,$flag) = @_;
  1465.  
  1466.     return( &_subtract($object,$argument,undef) );
  1467. }
  1468.  
  1469. sub _assign_multiply
  1470. {
  1471.     my($object,$argument,$flag) = @_;
  1472.  
  1473.     return( &_multiply($object,$argument,undef) );
  1474. }
  1475.  
  1476. sub _equal
  1477. {
  1478.     my($object,$argument,$flag) = @_;
  1479.     my($name) = "'=='"; #&_trace($name,$object,$argument,$flag);
  1480.     my($rows,$cols) = ($object->[1],$object->[2]);
  1481.     my($i,$j,$result);
  1482.  
  1483.     if ((defined $argument) && ref($argument) &&
  1484.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1485.     {
  1486.         $result = 1;
  1487.         EQUAL:
  1488.         for ( $i = 0; $i < $rows; $i++ )
  1489.         {
  1490.             for ( $j = 0; $j < $cols; $j++ )
  1491.             {
  1492.                 if ($object->[0][$i][$j] != $argument->[0][$i][$j])
  1493.                 {
  1494.                     $result = 0;
  1495.                     last EQUAL;
  1496.                 }
  1497.             }
  1498.         }
  1499.         return($result);
  1500.     }
  1501.     else
  1502.     {
  1503.         croak "Math::MatrixReal $name: wrong argument type";
  1504.     }
  1505. }
  1506.  
  1507. sub _not_equal
  1508. {
  1509.     my($object,$argument,$flag) = @_;
  1510.     my($name) = "'!='"; #&_trace($name,$object,$argument,$flag);
  1511.     my($rows,$cols) = ($object->[1],$object->[2]);
  1512.     my($i,$j,$result);
  1513.  
  1514.     if ((defined $argument) && ref($argument) &&
  1515.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1516.     {
  1517.         $result = 0;
  1518.         NOTEQUAL:
  1519.         for ( $i = 0; $i < $rows; $i++ )
  1520.         {
  1521.             for ( $j = 0; $j < $cols; $j++ )
  1522.             {
  1523.                 if ($object->[0][$i][$j] != $argument->[0][$i][$j])
  1524.                 {
  1525.                     $result = 1;
  1526.                     last NOTEQUAL;
  1527.                 }
  1528.             }
  1529.         }
  1530.         return($result);
  1531.     }
  1532.     else
  1533.     {
  1534.         croak "Math::MatrixReal $name: wrong argument type";
  1535.     }
  1536. }
  1537.  
  1538. sub _less_than
  1539. {
  1540.     my($object,$argument,$flag) = @_;
  1541.     my($name) = "'<'"; #&_trace($name,$object,$argument,$flag);
  1542.  
  1543.     if ((defined $argument) && ref($argument) &&
  1544.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1545.     {
  1546.         if ((defined $flag) && $flag)
  1547.         {
  1548.             return( $argument->norm_one() < $object->norm_one() );
  1549.         }
  1550.         else
  1551.         {
  1552.             return( $object->norm_one() < $argument->norm_one() );
  1553.         }
  1554.     }
  1555.     elsif ((defined $argument) && !(ref($argument)))
  1556.     {
  1557.         if ((defined $flag) && $flag)
  1558.         {
  1559.             return( abs($argument) < $object->norm_one() );
  1560.         }
  1561.         else
  1562.         {
  1563.             return( $object->norm_one() < abs($argument) );
  1564.         }
  1565.     }
  1566.     else
  1567.     {
  1568.         croak "Math::MatrixReal $name: wrong argument type";
  1569.     }
  1570. }
  1571.  
  1572. sub _less_than_or_equal
  1573. {
  1574.     my($object,$argument,$flag) = @_;
  1575.     my($name) = "'<='"; #&_trace($name,$object,$argument,$flag);
  1576.  
  1577.     if ((defined $argument) && ref($argument) &&
  1578.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1579.     {
  1580.         if ((defined $flag) && $flag)
  1581.         {
  1582.             return( $argument->norm_one() <= $object->norm_one() );
  1583.         }
  1584.         else
  1585.         {
  1586.             return( $object->norm_one() <= $argument->norm_one() );
  1587.         }
  1588.     }
  1589.     elsif ((defined $argument) && !(ref($argument)))
  1590.     {
  1591.         if ((defined $flag) && $flag)
  1592.         {
  1593.             return( abs($argument) <= $object->norm_one() );
  1594.         }
  1595.         else
  1596.         {
  1597.             return( $object->norm_one() <= abs($argument) );
  1598.         }
  1599.     }
  1600.     else
  1601.     {
  1602.         croak "Math::MatrixReal $name: wrong argument type";
  1603.     }
  1604. }
  1605.  
  1606. sub _greater_than
  1607. {
  1608.     my($object,$argument,$flag) = @_;
  1609.     my($name) = "'>'"; #&_trace($name,$object,$argument,$flag);
  1610.  
  1611.     if ((defined $argument) && ref($argument) &&
  1612.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1613.     {
  1614.         if ((defined $flag) && $flag)
  1615.         {
  1616.             return( $argument->norm_one() > $object->norm_one() );
  1617.         }
  1618.         else
  1619.         {
  1620.             return( $object->norm_one() > $argument->norm_one() );
  1621.         }
  1622.     }
  1623.     elsif ((defined $argument) && !(ref($argument)))
  1624.     {
  1625.         if ((defined $flag) && $flag)
  1626.         {
  1627.             return( abs($argument) > $object->norm_one() );
  1628.         }
  1629.         else
  1630.         {
  1631.             return( $object->norm_one() > abs($argument) );
  1632.         }
  1633.     }
  1634.     else
  1635.     {
  1636.         croak "Math::MatrixReal $name: wrong argument type";
  1637.     }
  1638. }
  1639.  
  1640. sub _greater_than_or_equal
  1641. {
  1642.     my($object,$argument,$flag) = @_;
  1643.     my($name) = "'>='"; #&_trace($name,$object,$argument,$flag);
  1644.  
  1645.     if ((defined $argument) && ref($argument) &&
  1646.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  1647.     {
  1648.         if ((defined $flag) && $flag)
  1649.         {
  1650.             return( $argument->norm_one() >= $object->norm_one() );
  1651.         }
  1652.         else
  1653.         {
  1654.             return( $object->norm_one() >= $argument->norm_one() );
  1655.         }
  1656.     }
  1657.     elsif ((defined $argument) && !(ref($argument)))
  1658.     {
  1659.         if ((defined $flag) && $flag)
  1660.         {
  1661.             return( abs($argument) >= $object->norm_one() );
  1662.         }
  1663.         else
  1664.         {
  1665.             return( $object->norm_one() >= abs($argument) );
  1666.         }
  1667.     }
  1668.     else
  1669.     {
  1670.         croak "Math::MatrixReal $name: wrong argument type";
  1671.     }
  1672. }
  1673.  
  1674. sub _clone
  1675. {
  1676.     my($object,$argument,$flag) = @_;
  1677.     my($temp);
  1678.  
  1679.     $temp = $object->new($object->[1],$object->[2]);
  1680.     $temp->copy($object);
  1681.     $temp->_undo_LR();
  1682.     return($temp);
  1683. }
  1684.  
  1685. sub _trace
  1686. {
  1687.     my($text,$object,$argument,$flag) = @_;
  1688.  
  1689.     unless (defined $object)   { $object   = 'undef'; };
  1690.     unless (defined $argument) { $argument = 'undef'; };
  1691.     unless (defined $flag)     { $flag     = 'undef'; };
  1692.     if (ref($object))   { $object   = ref($object);   }
  1693.     if (ref($argument)) { $argument = ref($argument); }
  1694.     print "$text: \$obj='$object' \$arg='$argument' \$flag='$flag'\n";
  1695. }
  1696.  
  1697. 1;
  1698.  
  1699. __END__
  1700.  
  1701. =head1 NAME
  1702.  
  1703. Math::MatrixReal - Matrix of Reals
  1704.  
  1705. Implements the data type "matrix of reals" (and consequently also
  1706. "vector of reals")
  1707.  
  1708. =head1 DESCRIPTION
  1709.  
  1710. Implements the data type "matrix of reals", which can be used almost
  1711. like any other basic Perl type thanks to B<OPERATOR OVERLOADING>, i.e.,
  1712.  
  1713.   $product = $matrix1 * $matrix2;
  1714.  
  1715. does what you would like it to do (a matrix multiplication).
  1716.  
  1717. Also features many important operations and methods: matrix norm,
  1718. matrix transposition, matrix inverse, determinant of a matrix, order
  1719. and numerical condition of a matrix, scalar product of vectors, vector
  1720. product of vectors, vector length, projection of row and column vectors,
  1721. a comfortable way for reading in a matrix from a file, the keyboard or
  1722. your code, and many more.
  1723.  
  1724. Allows to solve linear equation systems using an efficient algorithm
  1725. known as "LR decomposition" and several approximative (iterative) methods.
  1726.  
  1727. Features an implementation of Kleene's algorithm to compute the minimal
  1728. costs for all paths in a graph with weighted edges (the "weights" being
  1729. the costs associated with each edge).
  1730.  
  1731. =head1 SYNOPSIS
  1732.  
  1733. =over 2
  1734.  
  1735. =item *
  1736.  
  1737. C<use Math::MatrixReal;>
  1738.  
  1739. Makes the methods and overloaded operators of this module available
  1740. to your program.
  1741.  
  1742. =item *
  1743.  
  1744. C<use Math::MatrixReal qw(min max);>
  1745.  
  1746. =item *
  1747.  
  1748. C<use Math::MatrixReal qw(:all);>
  1749.  
  1750. Use one of these two variants to import (all) the functions which the module
  1751. offers for export; currently these are "min()" and "max()".
  1752.  
  1753. =item *
  1754.  
  1755. C<$new_matrix = new Math::MatrixReal($rows,$columns);>
  1756.  
  1757. The matrix object constructor method.
  1758.  
  1759. Note that this method is implicitly called by many of the other methods
  1760. in this module!
  1761.  
  1762. =item *
  1763.  
  1764. C<$new_matrix = Math::MatrixReal-E<gt>>C<new($rows,$columns);>
  1765.  
  1766. An alternate way of calling the matrix object constructor method.
  1767.  
  1768. =item *
  1769.  
  1770. C<$new_matrix = $some_matrix-E<gt>>C<new($rows,$columns);>
  1771.  
  1772. Still another way of calling the matrix object constructor method.
  1773.  
  1774. Matrix "C<$some_matrix>" is not changed by this in any way.
  1775.  
  1776. =item *
  1777.  
  1778. C<$new_matrix = Math::MatrixReal-E<gt>>C<new_from_string($string);>
  1779.  
  1780. This method allows you to read in a matrix from a string (for
  1781. instance, from the keyboard, from a file or from your code).
  1782.  
  1783. The syntax is simple: each row must start with "C<[ >" and end with
  1784. "C< ]\n>" ("C<\n>" being the newline character and "C< >" a space or
  1785. tab) and contain one or more numbers, all separated from each other
  1786. by spaces or tabs.
  1787.  
  1788. Additional spaces or tabs can be added at will, but no comments.
  1789.  
  1790. Examples:
  1791.  
  1792.   $string = "[ 1 2 3 ]\n[ 2 2 -1 ]\n[ 1 1 1 ]\n";
  1793.   $matrix = Math::MatrixReal->new_from_string($string);
  1794.   print "$matrix";
  1795.  
  1796. By the way, this prints
  1797.  
  1798.   [  1.000000000000E+00  2.000000000000E+00  3.000000000000E+00 ]
  1799.   [  2.000000000000E+00  2.000000000000E+00 -1.000000000000E+00 ]
  1800.   [  1.000000000000E+00  1.000000000000E+00  1.000000000000E+00 ]
  1801.  
  1802. But you can also do this in a much more comfortable way using the
  1803. shell-like "here-document" syntax:
  1804.  
  1805.   $matrix = Math::MatrixReal->new_from_string(<<'MATRIX');
  1806.   [  1  0  0  0  0  0  1  ]
  1807.   [  0  1  0  0  0  0  0  ]
  1808.   [  0  0  1  0  0  0  0  ]
  1809.   [  0  0  0  1  0  0  0  ]
  1810.   [  0  0  0  0  1  0  0  ]
  1811.   [  0  0  0  0  0  1  0  ]
  1812.   [  1  0  0  0  0  0 -1  ]
  1813.   MATRIX
  1814.  
  1815. You can even use variables in the matrix:
  1816.  
  1817.   $c1 =   2  /  3;
  1818.   $c2 =  -2  /  5;
  1819.   $c3 =  26  /  9;
  1820.  
  1821.   $matrix = Math::MatrixReal->new_from_string(<<"MATRIX");
  1822.  
  1823.       [   3    2    0   ]
  1824.       [   0    3    2   ]
  1825.       [  $c1  $c2  $c3  ]
  1826.  
  1827.   MATRIX
  1828.  
  1829. (Remember that you may use spaces and tabs to format the matrix to
  1830. your taste)
  1831.  
  1832. Note that this method uses exactly the same representation for a
  1833. matrix as the "stringify" operator "": this means that you can convert
  1834. any matrix into a string with C<$string = "$matrix";> and read it back
  1835. in later (for instance from a file!).
  1836.  
  1837. Note however that you may suffer a precision loss in this process
  1838. because only 13 digits are supported in the mantissa when printed!!
  1839.  
  1840. If the string you supply (or someone else supplies) does not obey
  1841. the syntax mentioned above, an exception is raised, which can be
  1842. caught by "eval" as follows:
  1843.  
  1844.   print "Please enter your matrix (in one line): ";
  1845.   $string = <STDIN>;
  1846.   $string =~ s/\\n/\n/g;
  1847.   eval { $matrix = Math::MatrixReal->new_from_string($string); };
  1848.   if ($@)
  1849.   {
  1850.       print "$@";
  1851.   }
  1852.   else
  1853.   {
  1854.   }
  1855.  
  1856. or as follows:
  1857.  
  1858.   eval { $matrix = Math::MatrixReal->new_from_string(<<"MATRIX"); };
  1859.   [   3    2    0   ]
  1860.   [   0    3    2   ]
  1861.   [  $c1  $c2  $c3  ]
  1862.   MATRIX
  1863.   if ($@)
  1864.  
  1865. Actually, the method shown above for reading a matrix from the keyboard
  1866. is a little awkward, since you have to enter a lot of "\n"'s for the
  1867. newlines.
  1868.  
  1869. A better way is shown in this piece of code:
  1870.  
  1871.   while (1)
  1872.   {
  1873.       print "\nPlease enter your matrix ";
  1874.       print "(multiple lines, <ctrl-D> = done):\n";
  1875.       eval { $new_matrix =
  1876.           Math::MatrixReal->new_from_string(join('',<STDIN>)); };
  1877.       if ($@)
  1878.       {
  1879.           $@ =~ s/\s+at\b.*?$//;
  1880.           print "${@}Please try again.\n";
  1881.       }
  1882.       else { last; }
  1883.   }
  1884.  
  1885. Possible error messages of the "new_from_string()" method are:
  1886.  
  1887.   Math::MatrixReal::new_from_string(): syntax error in input string
  1888.   Math::MatrixReal::new_from_string(): empty input string
  1889.  
  1890. If the input string has rows with varying numbers of columns,
  1891. the following warning will be printed to STDERR:
  1892.  
  1893.   Math::MatrixReal::new_from_string(): missing elements will be set to zero!
  1894.  
  1895. If everything is okay, the method returns an object reference to the
  1896. (newly allocated) matrix containing the elements you specified.
  1897.  
  1898. =item *
  1899.  
  1900. C<$new_matrix = $some_matrix-E<gt>shadow();>
  1901.  
  1902. Returns an object reference to a B<NEW> but B<EMPTY> matrix
  1903. (filled with zero's) of the B<SAME SIZE> as matrix "C<$some_matrix>".
  1904.  
  1905. Matrix "C<$some_matrix>" is not changed by this in any way.
  1906.  
  1907. =item *
  1908.  
  1909. C<$matrix1-E<gt>copy($matrix2);>
  1910.  
  1911. Copies the contents of matrix "C<$matrix2>" to an B<ALREADY EXISTING>
  1912. matrix "C<$matrix1>" (which must have the same size as matrix "C<$matrix2>"!).
  1913.  
  1914. Matrix "C<$matrix2>" is not changed by this in any way.
  1915.  
  1916. =item *
  1917.  
  1918. C<$twin_matrix = $some_matrix-E<gt>clone();>
  1919.  
  1920. Returns an object reference to a B<NEW> matrix of the B<SAME SIZE> as
  1921. matrix "C<$some_matrix>". The contents of matrix "C<$some_matrix>" have
  1922. B<ALREADY BEEN COPIED> to the new matrix "C<$twin_matrix>".
  1923.  
  1924. Matrix "C<$some_matrix>" is not changed by this in any way.
  1925.  
  1926. =item *
  1927.  
  1928. C<$row_vector = $matrix-E<gt>row($row);>
  1929.  
  1930. This is a projection method which returns an object reference to
  1931. a B<NEW> matrix (which in fact is a (row) vector since it has only
  1932. one row) to which row number "C<$row>" of matrix "C<$matrix>" has
  1933. already been copied.
  1934.  
  1935. Matrix "C<$matrix>" is not changed by this in any way.
  1936.  
  1937. =item *
  1938.  
  1939. C<$column_vector = $matrix-E<gt>column($column);>
  1940.  
  1941. This is a projection method which returns an object reference to
  1942. a B<NEW> matrix (which in fact is a (column) vector since it has
  1943. only one column) to which column number "C<$column>" of matrix
  1944. "C<$matrix>" has already been copied.
  1945.  
  1946. Matrix "C<$matrix>" is not changed by this in any way.
  1947.  
  1948. =item *
  1949.  
  1950. C<$matrix-E<gt>zero();>
  1951.  
  1952. Assigns a zero to every element of the matrix "C<$matrix>", i.e.,
  1953. erases all values previously stored there, thereby effectively
  1954. transforming the matrix into a "zero"-matrix or "null"-matrix,
  1955. the neutral element of the addition operation in a Ring.
  1956.  
  1957. (For instance the (quadratic) matrices with "n" rows and columns
  1958. and matrix addition and multiplication form a Ring. Most prominent
  1959. characteristic of a Ring is that multiplication is not commutative,
  1960. i.e., in general, "C<matrix1 * matrix2>" is not the same as
  1961. "C<matrix2 * matrix1>"!)
  1962.  
  1963. =item *
  1964.  
  1965. C<$matrix-E<gt>one();>
  1966.  
  1967. Assigns one's to the elements on the main diagonal (elements (1,1),
  1968. (2,2), (3,3) and so on) of matrix "C<$matrix>" and zero's to all others,
  1969. thereby erasing all values previously stored there and transforming the
  1970. matrix into a "one"-matrix, the neutral element of the multiplication
  1971. operation in a Ring.
  1972.  
  1973. (If the matrix is quadratic (which this method doesn't require, though),
  1974. then multiplying this matrix with itself yields this same matrix again,
  1975. and multiplying it with some other matrix leaves that other matrix
  1976. unchanged!)
  1977.  
  1978. =item *
  1979.  
  1980. C<$matrix-E<gt>assign($row,$column,$value);>
  1981.  
  1982. Explicitly assigns a value "C<$value>" to a single element of the
  1983. matrix "C<$matrix>", located in row "C<$row>" and column "C<$column>",
  1984. thereby replacing the value previously stored there.
  1985.  
  1986. =item *
  1987.  
  1988. C<$value = $matrix-E<gt>>C<element($row,$column);>
  1989.  
  1990. Returns the value of a specific element of the matrix "C<$matrix>",
  1991. located in row "C<$row>" and column "C<$column>".
  1992.  
  1993. =item *
  1994.  
  1995. C<($rows,$columns) = $matrix-E<gt>dim();>
  1996.  
  1997. Returns a list of two items, representing the number of rows
  1998. and columns the given matrix "C<$matrix>" contains.
  1999.  
  2000. =item *
  2001.  
  2002. C<$norm_one = $matrix-E<gt>norm_one();>
  2003.  
  2004. Returns the "one"-norm of the given matrix "C<$matrix>".
  2005.  
  2006. The "one"-norm is defined as follows:
  2007.  
  2008. For each column, the sum of the absolute values of the elements in the
  2009. different rows of that column is calculated. Finally, the maximum
  2010. of these sums is returned.
  2011.  
  2012. Note that the "one"-norm and the "maximum"-norm are mathematically
  2013. equivalent, although for the same matrix they usually yield a different
  2014. value.
  2015.  
  2016. Therefore, you should only compare values that have been calculated
  2017. using the same norm!
  2018.  
  2019. Throughout this package, the "one"-norm is (arbitrarily) used
  2020. for all comparisons, for the sake of uniformity and comparability,
  2021. except for the iterative methods "solve_GSM()", "solve_SSM()" and
  2022. "solve_RM()" which use either norm depending on the matrix itself.
  2023.  
  2024. =item *
  2025.  
  2026. C<$norm_max = $matrix-E<gt>norm_max();>
  2027.  
  2028. Returns the "maximum"-norm of the given matrix "C<$matrix>".
  2029.  
  2030. The "maximum"-norm is defined as follows:
  2031.  
  2032. For each row, the sum of the absolute values of the elements in the
  2033. different columns of that row is calculated. Finally, the maximum
  2034. of these sums is returned.
  2035.  
  2036. Note that the "maximum"-norm and the "one"-norm are mathematically
  2037. equivalent, although for the same matrix they usually yield a different
  2038. value.
  2039.  
  2040. Therefore, you should only compare values that have been calculated
  2041. using the same norm!
  2042.  
  2043. Throughout this package, the "one"-norm is (arbitrarily) used
  2044. for all comparisons, for the sake of uniformity and comparability,
  2045. except for the iterative methods "solve_GSM()", "solve_SSM()" and
  2046. "solve_RM()" which use either norm depending on the matrix itself.
  2047.  
  2048. =item *
  2049.  
  2050. C<$matrix1-E<gt>negate($matrix2);>
  2051.  
  2052. Calculates the negative of matrix "C<$matrix2>" (i.e., multiplies
  2053. all elements with "-1") and stores the result in matrix "C<$matrix1>"
  2054. (which must already exist and have the same size as matrix "C<$matrix2>"!).
  2055.  
  2056. This operation can also be carried out "in-place", i.e., input and
  2057. output matrix may be identical.
  2058.  
  2059. =item *
  2060.  
  2061. C<$matrix1-E<gt>transpose($matrix2);>
  2062.  
  2063. Calculates the transposed matrix of matrix "C<$matrix2>" and stores
  2064. the result in matrix "C<$matrix1>" (which must already exist and have
  2065. the same size as matrix "C<$matrix2>"!).
  2066.  
  2067. This operation can also be carried out "in-place", i.e., input and
  2068. output matrix may be identical.
  2069.  
  2070. Transposition is a symmetry operation: imagine you rotate the matrix
  2071. along the axis of its main diagonal (going through elements (1,1),
  2072. (2,2), (3,3) and so on) by 180 degrees.
  2073.  
  2074. Another way of looking at it is to say that rows and columns are
  2075. swapped. In fact the contents of element C<(i,j)> are swapped
  2076. with those of element C<(j,i)>.
  2077.  
  2078. Note that (especially for vectors) it makes a big difference if you
  2079. have a row vector, like this:
  2080.  
  2081.   [ -1 0 1 ]
  2082.  
  2083. or a column vector, like this:
  2084.  
  2085.   [ -1 ]
  2086.   [  0 ]
  2087.   [  1 ]
  2088.  
  2089. the one vector being the transposed of the other!
  2090.  
  2091. This is especially true for the matrix product of two vectors:
  2092.  
  2093.                [ -1 ]
  2094.   [ -1 0 1 ] * [  0 ]  =  [ 2 ] ,  whereas
  2095.                [  1 ]
  2096.  
  2097.                              *     [ -1  0  1 ]
  2098.   [ -1 ]                                            [  1  0 -1 ]
  2099.   [  0 ] * [ -1 0 1 ]  =  [ -1 ]   [  1  0 -1 ]  =  [  0  0  0 ]
  2100.   [  1 ]                  [  0 ]   [  0  0  0 ]     [ -1  0  1 ]
  2101.                           [  1 ]   [ -1  0  1 ]
  2102.  
  2103. So be careful about what you really mean!
  2104.  
  2105. Hint: throughout this module, whenever a vector is explicitly required
  2106. for input, a B<COLUMN> vector is expected!
  2107.  
  2108. =item *
  2109.  
  2110. C<$matrix1-E<gt>add($matrix2,$matrix3);>
  2111.  
  2112. Calculates the sum of matrix "C<$matrix2>" and matrix "C<$matrix3>"
  2113. and stores the result in matrix "C<$matrix1>" (which must already exist
  2114. and have the same size as matrix "C<$matrix2>" and matrix "C<$matrix3>"!).
  2115.  
  2116. This operation can also be carried out "in-place", i.e., the output and
  2117. one (or both) of the input matrices may be identical.
  2118.  
  2119. =item *
  2120.  
  2121. C<$matrix1-E<gt>subtract($matrix2,$matrix3);>
  2122.  
  2123. Calculates the difference of matrix "C<$matrix2>" minus matrix "C<$matrix3>"
  2124. and stores the result in matrix "C<$matrix1>" (which must already exist
  2125. and have the same size as matrix "C<$matrix2>" and matrix "C<$matrix3>"!).
  2126.  
  2127. This operation can also be carried out "in-place", i.e., the output and
  2128. one (or both) of the input matrices may be identical.
  2129.  
  2130. Note that this operation is the same as
  2131. C<$matrix1-E<gt>add($matrix2,-$matrix3);>, although the latter is
  2132. a little less efficient.
  2133.  
  2134. =item *
  2135.  
  2136. C<$matrix1-E<gt>multiply_scalar($matrix2,$scalar);>
  2137.  
  2138. Calculates the product of matrix "C<$matrix2>" and the number "C<$scalar>"
  2139. (i.e., multiplies each element of matrix "C<$matrix2>" with the factor
  2140. "C<$scalar>") and stores the result in matrix "C<$matrix1>" (which must
  2141. already exist and have the same size as matrix "C<$matrix2>"!).
  2142.  
  2143. This operation can also be carried out "in-place", i.e., input and
  2144. output matrix may be identical.
  2145.  
  2146. =item *
  2147.  
  2148. C<$product_matrix = $matrix1-E<gt>multiply($matrix2);>
  2149.  
  2150. Calculates the product of matrix "C<$matrix1>" and matrix "C<$matrix2>"
  2151. and returns an object reference to a new matrix "C<$product_matrix>" in
  2152. which the result of this operation has been stored.
  2153.  
  2154. Note that the dimensions of the two matrices "C<$matrix1>" and "C<$matrix2>"
  2155. (i.e., their numbers of rows and columns) must harmonize in the following
  2156. way (example):
  2157.  
  2158.                           [ 2 2 ]
  2159.                           [ 2 2 ]
  2160.                           [ 2 2 ]
  2161.  
  2162.               [ 1 1 1 ]   [ * * ]
  2163.               [ 1 1 1 ]   [ * * ]
  2164.               [ 1 1 1 ]   [ * * ]
  2165.               [ 1 1 1 ]   [ * * ]
  2166.  
  2167. I.e., the number of columns of matrix "C<$matrix1>" has to be the same
  2168. as the number of rows of matrix "C<$matrix2>".
  2169.  
  2170. The number of rows and columns of the resulting matrix "C<$product_matrix>"
  2171. is determined by the number of rows of matrix "C<$matrix1>" and the number
  2172. of columns of matrix "C<$matrix2>", respectively.
  2173.  
  2174. =item *
  2175.  
  2176. C<$minimum = Math::MatrixReal::min($number1,$number2);>
  2177.  
  2178. Returns the minimum of the two numbers "C<number1>" and "C<number2>".
  2179.  
  2180. =item *
  2181.  
  2182. C<$minimum = Math::MatrixReal::max($number1,$number2);>
  2183.  
  2184. Returns the maximum of the two numbers "C<number1>" and "C<number2>".
  2185.  
  2186. =item *
  2187.  
  2188. C<$minimal_cost_matrix = $cost_matrix-E<gt>kleene();>
  2189.  
  2190. Copies the matrix "C<$cost_matrix>" (which has to be quadratic!) to
  2191. a new matrix of the same size (i.e., "clones" the input matrix) and
  2192. applies Kleene's algorithm to it.
  2193.  
  2194. See L<Math::Kleene(3)> for more details about this algorithm!
  2195.  
  2196. The method returns an object reference to the new matrix.
  2197.  
  2198. Matrix "C<$cost_matrix>" is not changed by this method in any way.
  2199.  
  2200. =item *
  2201.  
  2202. C<($norm_matrix,$norm_vector) = $matrix-E<gt>normalize($vector);>
  2203.  
  2204. This method is used to improve the numerical stability when solving
  2205. linear equation systems.
  2206.  
  2207. Suppose you have a matrix "A" and a vector "b" and you want to find
  2208. out a vector "x" so that C<A * x = b>, i.e., the vector "x" which
  2209. solves the equation system represented by the matrix "A" and the
  2210. vector "b".
  2211.  
  2212. Applying this method to the pair (A,b) yields a pair (A',b') where
  2213. each row has been divided by (the absolute value of) the greatest
  2214. coefficient appearing in that row. So this coefficient becomes equal
  2215. to "1" (or "-1") in the new pair (A',b') (all others become smaller
  2216. than one and greater than minus one).
  2217.  
  2218. Note that this operation does not change the equation system itself
  2219. because the same division is carried out on either side of the equation
  2220. sign!
  2221.  
  2222. The method requires a quadratic (!) matrix "C<$matrix>" and a vector
  2223. "C<$vector>" for input (the vector must be a column vector with the same
  2224. number of rows as the input matrix) and returns a list of two items
  2225. which are object references to a new matrix and a new vector, in this
  2226. order.
  2227.  
  2228. The output matrix and vector are clones of the input matrix and vector
  2229. to which the operation explained above has been applied.
  2230.  
  2231. The input matrix and vector are not changed by this in any way.
  2232.  
  2233. Example of how this method can affect the result of the methods to solve
  2234. equation systems (explained immediately below following this method):
  2235.  
  2236. Consider the following little program:
  2237.  
  2238.  
  2239.   use Math::MatrixReal qw(new_from_string);
  2240.  
  2241.   $A = Math::MatrixReal->new_from_string(<<"MATRIX");
  2242.   [  1   2   3  ]
  2243.   [  5   7  11  ]
  2244.   [ 23  19  13  ]
  2245.   MATRIX
  2246.  
  2247.   $b = Math::MatrixReal->new_from_string(<<"MATRIX");
  2248.   [   0   ]
  2249.   [   1   ]
  2250.   [  29   ]
  2251.   MATRIX
  2252.  
  2253.   $LR = $A->decompose_LR();
  2254.   if (($dim,$x,$B) = $LR->solve_LR($b))
  2255.   {
  2256.       $test = $A * $x;
  2257.       print "x = \n$x";
  2258.       print "A * x = \n$test";
  2259.   }
  2260.  
  2261.   ($A_,$b_) = $A->normalize($b);
  2262.  
  2263.   $LR = $A_->decompose_LR();
  2264.   if (($dim,$x,$B) = $LR->solve_LR($b_))
  2265.   {
  2266.       $test = $A * $x;
  2267.       print "x = \n$x";
  2268.       print "A * x = \n$test";
  2269.   }
  2270.  
  2271. This will print:
  2272.  
  2273.   x =
  2274.   [  1.000000000000E+00 ]
  2275.   [  1.000000000000E+00 ]
  2276.   [ -1.000000000000E+00 ]
  2277.   A * x =
  2278.   [  4.440892098501E-16 ]
  2279.   [  1.000000000000E+00 ]
  2280.   [  2.900000000000E+01 ]
  2281.   x =
  2282.   [  1.000000000000E+00 ]
  2283.   [  1.000000000000E+00 ]
  2284.   [ -1.000000000000E+00 ]
  2285.   A * x =
  2286.   [  0.000000000000E+00 ]
  2287.   [  1.000000000000E+00 ]
  2288.   [  2.900000000000E+01 ]
  2289.  
  2290. You can see that in the second example (where "normalize()" has been used),
  2291. the result is "better", i.e., more accurate!
  2292.  
  2293. =item *
  2294.  
  2295. C<$LR_matrix = $matrix-E<gt>decompose_LR();>
  2296.  
  2297. This method is needed to solve linear equation systems.
  2298.  
  2299. Suppose you have a matrix "A" and a vector "b" and you want to find
  2300. out a vector "x" so that C<A * x = b>, i.e., the vector "x" which
  2301. solves the equation system represented by the matrix "A" and the
  2302. vector "b".
  2303.  
  2304. You might also have a matrix "A" and a whole bunch of different
  2305. vectors "b1".."bk" for which you need to find vectors "x1".."xk"
  2306. so that C<A * xi = bi>, for C<i=1..k>.
  2307.  
  2308. Using Gaussian transformations (multiplying a row or column with
  2309. a factor, swapping two rows or two columns and adding a multiple
  2310. of one row or column to another), it is possible to decompose any
  2311. matrix "A" into two triangular matrices, called "L" and "R" (for
  2312. "Left" and "Right").
  2313.  
  2314. "L" has one's on the main diagonal (the elements (1,1), (2,2), (3,3)
  2315. and so so), non-zero values to the left and below of the main diagonal
  2316. and all zero's in the upper right half of the matrix.
  2317.  
  2318. "R" has non-zero values on the main diagonal as well as to the right
  2319. and above of the main diagonal and all zero's in the lower left half
  2320. of the matrix, as follows:
  2321.  
  2322.           [ 1 0 0 0 0 ]      [ x x x x x ]
  2323.           [ x 1 0 0 0 ]      [ 0 x x x x ]
  2324.       L = [ x x 1 0 0 ]  R = [ 0 0 x x x ]
  2325.           [ x x x 1 0 ]      [ 0 0 0 x x ]
  2326.           [ x x x x 1 ]      [ 0 0 0 0 x ]
  2327.  
  2328. Note that "C<L * R>" is equivalent to matrix "A" in the sense that
  2329. C<L * R * x = b  E<lt>==E<gt>  A * x = b> for all vectors "x", leaving
  2330. out of account permutations of the rows and columns (these are taken
  2331. care of "magically" by this module!) and numerical errors.
  2332.  
  2333. Trick:
  2334.  
  2335. Because we know that "L" has one's on its main diagonal, we can
  2336. store both matrices together in the same array without information
  2337. loss! I.e.,
  2338.  
  2339.                  [ R R R R R ]
  2340.                  [ L R R R R ]
  2341.             LR = [ L L R R R ]
  2342.                  [ L L L R R ]
  2343.                  [ L L L L R ]
  2344.  
  2345. Beware, though, that "LR" and "C<L * R>" are not the same!!!
  2346.  
  2347. Note also that for the same reason, you cannot apply the method "normalize()"
  2348. to an "LR" decomposition matrix. Trying to do so will yield meaningless
  2349. rubbish!
  2350.  
  2351. (You need to apply "normalize()" to each pair (Ai,bi) B<BEFORE> decomposing
  2352. the matrix "Ai'"!)
  2353.  
  2354. Now what does all this help us in solving linear equation systems?
  2355.  
  2356. It helps us because a triangular matrix is the next best thing
  2357. that can happen to us besides a diagonal matrix (a matrix that
  2358. has non-zero values only on its main diagonal - in which case
  2359. the solution is trivial, simply divide "C<b[i]>" by "C<A[i,i]>"
  2360. to get "C<x[i]>"!).
  2361.  
  2362. To find the solution to our problem "C<A * x = b>", we divide this
  2363. problem in parts: instead of solving C<A * x = b> directly, we first
  2364. decompose "A" into "L" and "R" and then solve "C<L * y = b>" and
  2365. finally "C<R * x = y>" (motto: divide and rule!).
  2366.  
  2367. From the illustration above it is clear that solving "C<L * y = b>"
  2368. and "C<R * x = y>" is straightforward: we immediately know that
  2369. C<y[1] = b[1]>. We then deduce swiftly that
  2370.  
  2371.   y[2] = b[2] - L[2,1] * y[1]
  2372.  
  2373. (and we know "C<y[1]>" by now!), that
  2374.  
  2375.   y[3] = b[3] - L[3,1] * y[1] - L[3,2] * y[2]
  2376.  
  2377. and so on.
  2378.  
  2379. Having effortlessly calculated the vector "y", we now proceed to
  2380. calculate the vector "x" in a similar fashion: we see immediately
  2381. that C<x[n] = y[n] / R[n,n]>. It follows that
  2382.  
  2383.   x[n-1] = ( y[n-1] - R[n-1,n] * x[n] ) / R[n-1,n-1]
  2384.  
  2385. and
  2386.  
  2387.   x[n-2] = ( y[n-2] - R[n-2,n-1] * x[n-1] - R[n-2,n] * x[n] )
  2388.            / R[n-2,n-2]
  2389.  
  2390. and so on.
  2391.  
  2392. You can see that - especially when you have many vectors "b1".."bk"
  2393. for which you are searching solutions to C<A * xi = bi> - this scheme
  2394. is much more efficient than a straightforward, "brute force" approach.
  2395.  
  2396. This method requires a quadratic matrix as its input matrix.
  2397.  
  2398. If you don't have that many equations, fill up with zero's (i.e., do
  2399. nothing to fill the superfluous rows if it's a "fresh" matrix, i.e.,
  2400. a matrix that has been created with "new()" or "shadow()").
  2401.  
  2402. The method returns an object reference to a new matrix containing the
  2403. matrices "L" and "R".
  2404.  
  2405. The input matrix is not changed by this method in any way.
  2406.  
  2407. Note that you can "copy()" or "clone()" the result of this method without
  2408. losing its "magical" properties (for instance concerning the hidden
  2409. permutations of its rows and columns).
  2410.  
  2411. However, as soon as you are applying any method that alters the contents
  2412. of the matrix, its "magical" properties are stripped off, and the matrix
  2413. immediately reverts to an "ordinary" matrix (with the values it just happens
  2414. to contain at that moment, be they meaningful as an ordinary matrix or not!).
  2415.  
  2416. =item *
  2417.  
  2418. C<($dimension,$x_vector,$base_matrix) = $LR_matrix>C<-E<gt>>C<solve_LR($b_vector);>
  2419.  
  2420. Use this method to actually solve an equation system.
  2421.  
  2422. Matrix "C<$LR_matrix>" must be a (quadratic) matrix returned by the
  2423. method "decompose_LR()", the LR decomposition matrix of the matrix
  2424. "A" of your equation system C<A * x = b>.
  2425.  
  2426. The input vector "C<$b_vector>" is the vector "b" in your equation system
  2427. C<A * x = b>, which must be a column vector and have the same number of
  2428. rows as the input matrix "C<$LR_matrix>".
  2429.  
  2430. The method returns a list of three items if a solution exists or an
  2431. empty list otherwise (!).
  2432.  
  2433. Therefore, you should always use this method like this:
  2434.  
  2435.   if ( ($dim,$x_vec,$base) = $LR->solve_LR($b_vec) )
  2436.   {
  2437.   }
  2438.   else
  2439.   {
  2440.   }
  2441.  
  2442. The three items returned are: the dimension "C<$dimension>" of the solution
  2443. space (which is zero if only one solution exists, one if the solution is
  2444. a straight line, two if the solution is a plane, and so on), the solution
  2445. vector "C<$x_vector>" (which is the vector "x" of your equation system
  2446. C<A * x = b>) and a matrix "C<$base_matrix>" representing a base of the
  2447. solution space (a set of vectors which put up the solution space like
  2448. the spokes of an umbrella).
  2449.  
  2450. Only the first "C<$dimension>" columns of this base matrix actually
  2451. contain entries, the remaining columns are all zero.
  2452.  
  2453. Now what is all this stuff with that "base" good for?
  2454.  
  2455. The output vector "x" is B<ALWAYS> a solution of your equation system
  2456. C<A * x = b>.
  2457.  
  2458. But also any vector "C<$vector>"
  2459.  
  2460.   $vector = $x_vector->clone();
  2461.  
  2462.   $machine_infinity = 1E+99; # or something like that
  2463.  
  2464.   for ( $i = 1; $i <= $dimension; $i++ )
  2465.   {
  2466.       $vector += rand($machine_infinity) * $base_matrix->column($i);
  2467.   }
  2468.  
  2469. is a solution to your problem C<A * x = b>, i.e., if "C<$A_matrix>" contains
  2470. your matrix "A", then
  2471.  
  2472.   print abs( $A_matrix * $vector - $b_vector ), "\n";
  2473.  
  2474. should print a number around 1E-16 or so!
  2475.  
  2476. By the way, note that you can actually calculate those vectors "C<$vector>"
  2477. a little more efficient as follows:
  2478.  
  2479.   $rand_vector = $x_vector->shadow();
  2480.  
  2481.   $machine_infinity = 1E+99; # or something like that
  2482.  
  2483.   for ( $i = 1; $i <= $dimension; $i++ )
  2484.   {
  2485.       $rand_vector->assign($i,1, rand($machine_infinity) );
  2486.   }
  2487.  
  2488.   $vector = $x_vector + ( $base_matrix * $rand_vector );
  2489.  
  2490. Note that the input matrix and vector are not changed by this method
  2491. in any way.
  2492.  
  2493. =item *
  2494.  
  2495. C<$inverse_matrix = $LR_matrix-E<gt>invert_LR();>
  2496.  
  2497. Use this method to calculate the inverse of a given matrix "C<$LR_matrix>",
  2498. which must be a (quadratic) matrix returned by the method "decompose_LR()".
  2499.  
  2500. The method returns an object reference to a new matrix of the same size as
  2501. the input matrix containing the inverse of the matrix that you initially
  2502. fed into "decompose_LR()" B<IF THE INVERSE EXISTS>, or an empty list
  2503. otherwise.
  2504.  
  2505. Therefore, you should always use this method in the following way:
  2506.  
  2507.   if ( $inverse_matrix = $LR->invert_LR() )
  2508.   {
  2509.   }
  2510.   else
  2511.   {
  2512.   }
  2513.  
  2514. Note that by definition (disregarding numerical errors), the product
  2515. of the initial matrix and its inverse (or vice-versa) is always a matrix
  2516. containing one's on the main diagonal (elements (1,1), (2,2), (3,3) and
  2517. so on) and zero's elsewhere.
  2518.  
  2519. The input matrix is not changed by this method in any way.
  2520.  
  2521. =item *
  2522.  
  2523. C<$condition = $matrix-E<gt>condition($inverse_matrix);>
  2524.  
  2525. In fact this method is just a shortcut for
  2526.  
  2527.   abs($matrix) * abs($inverse_matrix)
  2528.  
  2529. Both input matrices must be quadratic and have the same size, and the result
  2530. is meaningful only if one of them is the inverse of the other (for instance,
  2531. as returned by the method "invert_LR()").
  2532.  
  2533. The number returned is a measure of the "condition" of the given matrix
  2534. "C<$matrix>", i.e., a measure of the numerical stability of the matrix.
  2535.  
  2536. This number is always positive, and the smaller its value, the better the
  2537. condition of the matrix (the better the stability of all subsequent
  2538. computations carried out using this matrix).
  2539.  
  2540. Numerical stability means for example that if
  2541.  
  2542.   abs( $vec_correct - $vec_with_error ) < $epsilon
  2543.  
  2544. holds, there must be a "C<$delta>" which doesn't depend on the vector
  2545. "C<$vec_correct>" (nor "C<$vec_with_error>", by the way) so that
  2546.  
  2547.   abs( $matrix * $vec_correct - $matrix * $vec_with_error ) < $delta
  2548.  
  2549. also holds.
  2550.  
  2551. =item *
  2552.  
  2553. C<$determinant = $LR_matrix-E<gt>det_LR();>
  2554.  
  2555. Calculates the determinant of a matrix, whose LR decomposition matrix
  2556. "C<$LR_matrix>" must be given (which must be a (quadratic) matrix
  2557. returned by the method "decompose_LR()").
  2558.  
  2559. In fact the determinant is a by-product of the LR decomposition: It is
  2560. (in principle, that is, except for the sign) simply the product of the
  2561. elements on the main diagonal (elements (1,1), (2,2), (3,3) and so on)
  2562. of the LR decomposition matrix.
  2563.  
  2564. (The sign is taken care of "magically" by this module)
  2565.  
  2566. =item *
  2567.  
  2568. C<$order = $LR_matrix-E<gt>order_LR();>
  2569.  
  2570. Calculates the order (called "Rang" in German) of a matrix, whose
  2571. LR decomposition matrix "C<$LR_matrix>" must be given (which must
  2572. be a (quadratic) matrix returned by the method "decompose_LR()").
  2573.  
  2574. This number is a measure of the number of linear independent row
  2575. and column vectors (= number of linear independent equations in
  2576. the case of a matrix representing an equation system) of the
  2577. matrix that was initially fed into "decompose_LR()".
  2578.  
  2579. If "n" is the number of rows and columns of the (quadratic!) matrix,
  2580. then "n - order" is the dimension of the solution space of the
  2581. associated equation system.
  2582.  
  2583. =item *
  2584.  
  2585. C<$scalar_product = $vector1-E<gt>scalar_product($vector2);>
  2586.  
  2587. Returns the scalar product of vector "C<$vector1>" and vector "C<$vector2>".
  2588.  
  2589. Both vectors must be column vectors (i.e., a matrix having
  2590. several rows but only one column).
  2591.  
  2592. This is a (more efficient!) shortcut for
  2593.  
  2594.   $temp           = ~$vector1 * $vector2;
  2595.   $scalar_product =  $temp->element(1,1);
  2596.  
  2597. or the sum C<i=1..n> of the products C<vector1[i] * vector2[i]>.
  2598.  
  2599. Provided none of the two input vectors is the null vector, then
  2600. the two vectors are orthogonal, i.e., have an angle of 90 degrees
  2601. between them, exactly when their scalar product is zero, and
  2602. vice-versa.
  2603.  
  2604. =item *
  2605.  
  2606. C<$vector_product = $vector1-E<gt>vector_product($vector2);>
  2607.  
  2608. Returns the vector product of vector "C<$vector1>" and vector "C<$vector2>".
  2609.  
  2610. Both vectors must be column vectors (i.e., a matrix having several rows
  2611. but only one column).
  2612.  
  2613. Currently, the vector product is only defined for 3 dimensions (i.e.,
  2614. vectors with 3 rows); all other vectors trigger an error message.
  2615.  
  2616. In 3 dimensions, the vector product of two vectors "x" and "y"
  2617. is defined as
  2618.  
  2619.               |  x[1]  y[1]  e[1]  |
  2620.   determinant |  x[2]  y[2]  e[2]  |
  2621.               |  x[3]  y[3]  e[3]  |
  2622.  
  2623. where the "C<x[i]>" and "C<y[i]>" are the components of the two vectors
  2624. "x" and "y", respectively, and the "C<e[i]>" are unity vectors (i.e.,
  2625. vectors with a length equal to one) with a one in row "i" and zero's
  2626. elsewhere (this means that you have numbers and vectors as elements
  2627. in this matrix!).
  2628.  
  2629. This determinant evaluates to the rather simple formula
  2630.  
  2631.   z[1] = x[2] * y[3] - x[3] * y[2]
  2632.   z[2] = x[3] * y[1] - x[1] * y[3]
  2633.   z[3] = x[1] * y[2] - x[2] * y[1]
  2634.  
  2635. A characteristic property of the vector product is that the resulting
  2636. vector is orthogonal to both of the input vectors (if neither of both
  2637. is the null vector, otherwise this is trivial), i.e., the scalar product
  2638. of each of the input vectors with the resulting vector is always zero.
  2639.  
  2640. =item *
  2641.  
  2642. C<$length = $vector-E<gt>length();>
  2643.  
  2644. This is actually a shortcut for
  2645.  
  2646.   $length = sqrt( $vector->scalar_product($vector) );
  2647.  
  2648. and returns the length of a given (column!) vector "C<$vector>".
  2649.  
  2650. Note that the "length" calculated by this method is in fact the
  2651. "two"-norm of a vector "C<$vector>"!
  2652.  
  2653. The general definition for norms of vectors is the following:
  2654.  
  2655.   sub vector_norm
  2656.   {
  2657.       croak "Usage: \$norm = \$vector->vector_norm(\$n);"
  2658.         if (@_ != 2);
  2659.  
  2660.       my($vector,$n) = @_;
  2661.       my($rows,$cols) = ($vector->[1],$vector->[2]);
  2662.       my($k,$comp,$sum);
  2663.  
  2664.       croak "Math::MatrixReal::vector_norm(): vector is not a column vector"
  2665.         unless ($cols == 1);
  2666.  
  2667.       croak "Math::MatrixReal::vector_norm(): norm index must be > 0"
  2668.         unless ($n > 0);
  2669.  
  2670.       croak "Math::MatrixReal::vector_norm(): norm index must be integer"
  2671.         unless ($n == int($n));
  2672.  
  2673.       $sum = 0;
  2674.       for ( $k = 0; $k < $rows; $k++ )
  2675.       {
  2676.           $comp = abs( $vector->[0][$k][0] );
  2677.           $sum += $comp ** $n;
  2678.       }
  2679.       return( $sum ** (1 / $n) );
  2680.   }
  2681.  
  2682. Note that the case "n = 1" is the "one"-norm for matrices applied to a
  2683. vector, the case "n = 2" is the euclidian norm or length of a vector,
  2684. and if "n" goes to infinity, you have the "infinity"- or "maximum"-norm
  2685. for matrices applied to a vector!
  2686.  
  2687. =item *
  2688.  
  2689. C<$xn_vector = $matrix-E<gt>>C<solve_GSM($x0_vector,$b_vector,$epsilon);>
  2690.  
  2691. =item *
  2692.  
  2693. C<$xn_vector = $matrix-E<gt>>C<solve_SSM($x0_vector,$b_vector,$epsilon);>
  2694.  
  2695. =item *
  2696.  
  2697. C<$xn_vector = $matrix-E<gt>>C<solve_RM($x0_vector,$b_vector,$weight,$epsilon);>
  2698.  
  2699. In some cases it might not be practical or desirable to solve an
  2700. equation system "C<A * x = b>" using an analytical algorithm like
  2701. the "decompose_LR()" and "solve_LR()" method pair.
  2702.  
  2703. In fact in some cases, due to the numerical properties (the "condition")
  2704. of the matrix "A", the numerical error of the obtained result can be
  2705. greater than by using an approximative (iterative) algorithm like one
  2706. of the three implemented here.
  2707.  
  2708. All three methods, GSM ("Global Step Method" or "Gesamtschrittverfahren"),
  2709. SSM ("Single Step Method" or "Einzelschrittverfahren") and RM ("Relaxation
  2710. Method" or "Relaxationsverfahren"), are fix-point iterations, that is, can
  2711. be described by an iteration function "C<x(t+1) = Phi( x(t) )>" which has
  2712. the property:
  2713.  
  2714.   Phi(x)  =  x    <==>    A * x  =  b
  2715.  
  2716. We can define "C<Phi(x)>" as follows:
  2717.  
  2718.   Phi(x)  :=  ( En - A ) * x  +  b
  2719.  
  2720. where "En" is a matrix of the same size as "A" ("n" rows and columns)
  2721. with one's on its main diagonal and zero's elsewhere.
  2722.  
  2723. This function has the required property.
  2724.  
  2725. Proof:
  2726.  
  2727.            A * x        =   b
  2728.  
  2729.   <==>  -( A * x )      =  -b
  2730.  
  2731.   <==>  -( A * x ) + x  =  -b + x
  2732.  
  2733.   <==>  -( A * x ) + x + b  =  x
  2734.  
  2735.   <==>  x - ( A * x ) + b  =  x
  2736.  
  2737.   <==>  ( En - A ) * x + b  =  x
  2738.  
  2739. This last step is true because
  2740.  
  2741.   x[i] - ( a[i,1] x[1] + ... + a[i,i] x[i] + ... + a[i,n] x[n] ) + b[i]
  2742.  
  2743. is the same as
  2744.  
  2745.   ( -a[i,1] x[1] + ... + (1 - a[i,i]) x[i] + ... + -a[i,n] x[n] ) + b[i]
  2746.  
  2747. qed
  2748.  
  2749. Note that actually solving the equation system "C<A * x = b>" means
  2750. to calculate
  2751.  
  2752.         a[i,1] x[1] + ... + a[i,i] x[i] + ... + a[i,n] x[n]  =  b[i]
  2753.  
  2754.   <==>  a[i,i] x[i]  =
  2755.         b[i]
  2756.         - ( a[i,1] x[1] + ... + a[i,i] x[i] + ... + a[i,n] x[n] )
  2757.         + a[i,i] x[i]
  2758.  
  2759.   <==>  x[i]  =
  2760.         ( b[i]
  2761.             - ( a[i,1] x[1] + ... + a[i,i] x[i] + ... + a[i,n] x[n] )
  2762.             + a[i,i] x[i]
  2763.         ) / a[i,i]
  2764.  
  2765.   <==>  x[i]  =
  2766.         ( b[i] -
  2767.             ( a[i,1] x[1] + ... + a[i,i-1] x[i-1] +
  2768.               a[i,i+1] x[i+1] + ... + a[i,n] x[n] )
  2769.         ) / a[i,i]
  2770.  
  2771. There is one major restriction, though: a fix-point iteration is
  2772. guaranteed to converge only if the first derivative of the iteration
  2773. function has an absolute value less than one in an area around the
  2774. point "C<x(*)>" for which "C<Phi( x(*) ) = x(*)>" is to be true, and
  2775. if the start vector "C<x(0)>" lies within that area!
  2776.  
  2777. This is best verified grafically, which unfortunately is impossible
  2778. to do in this textual documentation!
  2779.  
  2780. See literature on Numerical Analysis for details!
  2781.  
  2782. In our case, this restriction translates to the following three conditions:
  2783.  
  2784. There must exist a norm so that the norm of the matrix of the iteration
  2785. function, C<( En - A )>, has a value less than one, the matrix "A" may
  2786. not have any zero value on its main diagonal and the initial vector
  2787. "C<x(0)>" must be "good enough", i.e., "close enough" to the solution
  2788. "C<x(*)>".
  2789.  
  2790. (Remember school math: the first derivative of a straight line given by
  2791. "C<y = a * x + b>" is "a"!)
  2792.  
  2793. The three methods expect a (quadratic!) matrix "C<$matrix>" as their
  2794. first argument, a start vector "C<$x0_vector>", a vector "C<$b_vector>"
  2795. (which is the vector "b" in your equation system "C<A * x = b>"), in the
  2796. case of the "Relaxation Method" ("RM"), a real number "C<$weight>" best
  2797. between zero and two, and finally an error limit (real number) "C<$epsilon>".
  2798.  
  2799. (Note that the weight "C<$weight>" used by the "Relaxation Method" ("RM")
  2800. is B<NOT> checked to lie within any reasonable range!)
  2801.  
  2802. The three methods first test the first two conditions of the three
  2803. conditions listed above and return an empty list if these conditions
  2804. are not fulfilled.
  2805.  
  2806. Therefore, you should always test their return value using some
  2807. code like:
  2808.  
  2809.   if ( $xn_vector = $A_matrix->solve_GSM($x0_vector,$b_vector,1E-12) )
  2810.   {
  2811.   }
  2812.   else
  2813.   {
  2814.   }
  2815.  
  2816. Otherwise, they iterate until C<abs( Phi(x) - x ) E<lt> epsilon>.
  2817.  
  2818. (Beware that theoretically, infinite loops might result if the starting
  2819. vector is too far "off" the solution! In practice, this shouldn't be
  2820. a problem. Anyway, you can always press <ctrl-C> if you think that the
  2821. iteration takes too long!)
  2822.  
  2823. The difference between the three methods is the following:
  2824.  
  2825. In the "Global Step Method" ("GSM"), the new vector "C<x(t+1)>"
  2826. (called "y" here) is calculated from the vector "C<x(t)>"
  2827. (called "x" here) according to the formula:
  2828.  
  2829.   y[i] =
  2830.   ( b[i]
  2831.       - ( a[i,1] x[1] + ... + a[i,i-1] x[i-1] +
  2832.           a[i,i+1] x[i+1] + ... + a[i,n] x[n] )
  2833.   ) / a[i,i]
  2834.  
  2835. In the "Single Step Method" ("SSM"), the components of the vector
  2836. "C<x(t+1)>" which have already been calculated are used to calculate
  2837. the remaining components, i.e.
  2838.  
  2839.   y[i] =
  2840.   ( b[i]
  2841.       - ( a[i,1] y[1] + ... + a[i,i-1] y[i-1] +  # note the "y[]"!
  2842.           a[i,i+1] x[i+1] + ... + a[i,n] x[n] )  # note the "x[]"!
  2843.   ) / a[i,i]
  2844.  
  2845. In the "Relaxation method" ("RM"), the components of the vector
  2846. "C<x(t+1)>" are calculated by "mixing" old and new value (like
  2847. cold and hot water), and the weight "C<$weight>" determines the
  2848. "aperture" of both the "hot water tap" as well as of the "cold
  2849. water tap", according to the formula:
  2850.  
  2851.   y[i] =
  2852.   ( b[i]
  2853.       - ( a[i,1] y[1] + ... + a[i,i-1] y[i-1] +  # note the "y[]"!
  2854.           a[i,i+1] x[i+1] + ... + a[i,n] x[n] )  # note the "x[]"!
  2855.   ) / a[i,i]
  2856.   y[i] = weight * y[i] + (1 - weight) * x[i]
  2857.  
  2858. Note that the weight "C<$weight>" should be greater than zero and
  2859. less than two (!).
  2860.  
  2861. The three methods are supposed to be of different efficiency.
  2862. Experiment!
  2863.  
  2864. Remember that in most cases, it is probably advantageous to first
  2865. "normalize()" your equation system prior to solving it!
  2866.  
  2867. =back
  2868.  
  2869. =head1 OVERLOADED OPERATORS
  2870.  
  2871. =head2 SYNOPSIS
  2872.  
  2873. =over 2
  2874.  
  2875. =item *
  2876.  
  2877. Unary operators:
  2878.  
  2879. "C<->", "C<~>", "C<abs>", C<test>, "C<!>", 'C<"">'
  2880.  
  2881. =item *
  2882.  
  2883. Binary (arithmetic) operators:
  2884.  
  2885. "C<+>", "C<->", "C<*>"
  2886.  
  2887. =item *
  2888.  
  2889. Binary (relational) operators:
  2890.  
  2891. "C<==>", "C<!=>", "C<E<lt>>", "C<E<lt>=>", "C<E<gt>>", "C<E<gt>=>"
  2892.  
  2893. "C<eq>", "C<ne>", "C<lt>", "C<le>", "C<gt>", "C<ge>"
  2894.  
  2895. Note that the latter ("C<eq>", "C<ne>", ... ) are just synonyms
  2896. of the former ("C<==>", "C<!=>", ... ), defined for convenience
  2897. only.
  2898.  
  2899. =back
  2900.  
  2901. =head2 DESCRIPTION
  2902.  
  2903. =over 5
  2904.  
  2905. =item '-'
  2906.  
  2907. Unary minus
  2908.  
  2909. Returns the negative of the given matrix, i.e., the matrix with
  2910. all elements multiplied with the factor "-1".
  2911.  
  2912. Example:
  2913.  
  2914.     $matrix = -$matrix;
  2915.  
  2916. =item '~'
  2917.  
  2918. Transposition
  2919.  
  2920. Returns the transposed of the given matrix.
  2921.  
  2922. Examples:
  2923.  
  2924.     $temp = ~$vector * $vector;
  2925.     $length = sqrt( $temp->element(1,1) );
  2926.  
  2927.     if (~$matrix == $matrix) { # matrix is symmetric ... }
  2928.  
  2929. =item abs
  2930.  
  2931. Norm
  2932.  
  2933. Returns the "one"-Norm of the given matrix.
  2934.  
  2935. Example:
  2936.  
  2937.     $error = abs( $A * $x - $b );
  2938.  
  2939. =item test
  2940.  
  2941. Boolean test
  2942.  
  2943. Tests wether there is at least one non-zero element in the matrix.
  2944.  
  2945. Example:
  2946.  
  2947.     if ($xn_vector) { # result of iteration is not zero ... }
  2948.  
  2949. =item '!'
  2950.  
  2951. Negated boolean test
  2952.  
  2953. Tests wether the matrix contains only zero's.
  2954.  
  2955. Examples:
  2956.  
  2957.     if (! $b_vector) { # heterogenous equation system ... }
  2958.     else             { # homogenous equation system ... }
  2959.  
  2960.     unless ($x_vector) { # not the null-vector! }
  2961.  
  2962. =item '""""'
  2963.  
  2964. "Stringify" operator
  2965.  
  2966. Converts the given matrix into a string.
  2967.  
  2968. Uses scientific representation to keep precision loss to a minimum in case
  2969. you want to read this string back in again later with "new_from_string()".
  2970.  
  2971. Uses a 13-digit mantissa and a 20-character field for each element so that
  2972. lines will wrap nicely on an 80-column screen.
  2973.  
  2974. Examples:
  2975.  
  2976.     $matrix = Math::MatrixReal->new_from_string(<<"MATRIX");
  2977.     [ 1  0 ]
  2978.     [ 0 -1 ]
  2979.     MATRIX
  2980.     print "$matrix";
  2981.  
  2982.     [  1.000000000000E+00  0.000000000000E+00 ]
  2983.     [  0.000000000000E+00 -1.000000000000E+00 ]
  2984.  
  2985.     $string = "$matrix";
  2986.     $test = Math::MatrixReal->new_from_string($string);
  2987.     if ($test == $matrix) { print ":-)\n"; } else { print ":-(\n"; }
  2988.  
  2989. =item '+'
  2990.  
  2991. Addition
  2992.  
  2993. Returns the sum of the two given matrices.
  2994.  
  2995. Examples:
  2996.  
  2997.     $matrix_S = $matrix_A + $matrix_B;
  2998.  
  2999.     $matrix_A += $matrix_B;
  3000.  
  3001. =item '-'
  3002.  
  3003. Subtraction
  3004.  
  3005. Returns the difference of the two given matrices.
  3006.  
  3007. Examples:
  3008.  
  3009.     $matrix_D = $matrix_A - $matrix_B;
  3010.  
  3011.     $matrix_A -= $matrix_B;
  3012.  
  3013. Note that this is the same as:
  3014.  
  3015.     $matrix_S = $matrix_A + -$matrix_B;
  3016.  
  3017.     $matrix_A += -$matrix_B;
  3018.  
  3019. (The latter are less efficient, though)
  3020.  
  3021. =item '*'
  3022.  
  3023. Multiplication
  3024.  
  3025. Returns the matrix product of the two given matrices or
  3026. the product of the given matrix and scalar factor.
  3027.  
  3028. Examples:
  3029.  
  3030.     $matrix_P = $matrix_A * $matrix_B;
  3031.  
  3032.     $matrix_A *= $matrix_B;
  3033.  
  3034.     $vector_b = $matrix_A * $vector_x;
  3035.  
  3036.     $matrix_B = -1 * $matrix_A;
  3037.  
  3038.     $matrix_B = $matrix_A * -1;
  3039.  
  3040.     $matrix_A *= -1;
  3041.  
  3042. =item '=='
  3043.  
  3044. Equality
  3045.  
  3046. Tests two matrices for equality.
  3047.  
  3048. Example:
  3049.  
  3050.     if ( $A * $x == $b ) { print "EUREKA!\n"; }
  3051.  
  3052. Note that in most cases, due to numerical errors (due to the finite
  3053. precision of computer arithmetics), it is a bad idea to compare two
  3054. matrices or vectors this way.
  3055.  
  3056. Better use the norm of the difference of the two matrices you want
  3057. to compare and compare that norm with a small number, like this:
  3058.  
  3059.     if ( abs( $A * $x - $b ) < 1E-12 ) { print "BINGO!\n"; }
  3060.  
  3061. =item '!='
  3062.  
  3063. Inequality
  3064.  
  3065. Tests two matrices for inequality.
  3066.  
  3067. Example:
  3068.  
  3069.     while ($x0_vector != $xn_vector) { # proceed with iteration ... }
  3070.  
  3071. (Stops when the iteration becomes stationary)
  3072.  
  3073. Note that (just like with the '==' operator), it is usually a bad idea
  3074. to compare matrices or vectors this way. Compare the norm of the difference
  3075. of the two matrices with a small number instead.
  3076.  
  3077. =item 'E<lt>'
  3078.  
  3079. Less than
  3080.  
  3081. Examples:
  3082.  
  3083.     if ( $matrix1 < $matrix2 ) { # ... }
  3084.  
  3085.     if ( $vector < $epsilon ) { # ... }
  3086.  
  3087.     if ( 1E-12 < $vector ) { # ... }
  3088.  
  3089.     if ( $A * $x - $b < 1E-12 ) { # ... }
  3090.  
  3091. These are just shortcuts for saying:
  3092.  
  3093.     if ( abs($matrix1) < abs($matrix2) ) { # ... }
  3094.  
  3095.     if ( abs($vector) < abs($epsilon) ) { # ... }
  3096.  
  3097.     if ( abs(1E-12) < abs($vector) ) { # ... }
  3098.  
  3099.     if ( abs( $A * $x - $b ) < abs(1E-12) ) { # ... }
  3100.  
  3101. Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.
  3102.  
  3103. =item 'E<lt>='
  3104.  
  3105. Less than or equal
  3106.  
  3107. As with the '<' operator, this is just a shortcut for the same expression
  3108. with "abs()" around all arguments.
  3109.  
  3110. Example:
  3111.  
  3112.     if ( $A * $x - $b <= 1E-12 ) { # ... }
  3113.  
  3114. which in fact is the same as:
  3115.  
  3116.     if ( abs( $A * $x - $b ) <= abs(1E-12) ) { # ... }
  3117.  
  3118. Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.
  3119.  
  3120. =item 'E<gt>'
  3121.  
  3122. Greater than
  3123.  
  3124. As with the '<' and '<=' operator, this
  3125.  
  3126.     if ( $xn - $x0 > 1E-12 ) { # ... }
  3127.  
  3128. is just a shortcut for:
  3129.  
  3130.     if ( abs( $xn - $x0 ) > abs(1E-12) ) { # ... }
  3131.  
  3132. Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.
  3133.  
  3134. =item 'E<gt>='
  3135.  
  3136. Greater than or equal
  3137.  
  3138. As with the '<', '<=' and '>' operator, the following
  3139.  
  3140.     if ( $LR >= $A ) { # ... }
  3141.  
  3142. is simply a shortcut for:
  3143.  
  3144.     if ( abs($LR) >= abs($A) ) { # ... }
  3145.  
  3146. Uses the "one"-norm for matrices and Perl's built-in "abs()" for scalars.
  3147.  
  3148. =back
  3149.  
  3150. =head1 SEE ALSO
  3151.  
  3152. Math::MatrixBool(3), DFA::Kleene(3), Math::Kleene(3),
  3153. Set::IntegerRange(3), Set::IntegerFast(3), Bit::Vector(3).
  3154.  
  3155. =head1 VERSION
  3156.  
  3157. This man page documents "Math::MatrixReal" version 1.2.
  3158.  
  3159. =head1 AUTHOR
  3160.  
  3161. Steffen Beyer <sb@sdm.de>.
  3162.  
  3163. =head1 CREDITS
  3164.  
  3165. Many thanks to Prof. Pahlings for stoking the fire of my enthusiasm for
  3166. Algebra and Linear Algebra at the university (RWTH Aachen, Germany), and
  3167. to Prof. Esser and his assistant, Mr. Jarausch, for their fascinating
  3168. lectures in Numerical Analysis!
  3169.  
  3170. =head1 COPYRIGHT
  3171.  
  3172. Copyright (c) 1996, 1997 by Steffen Beyer. All rights reserved.
  3173.  
  3174. =head1 LICENSE AGREEMENT
  3175.  
  3176. This package is free software; you can redistribute it and/or
  3177. modify it under the same terms as Perl itself.
  3178.  
  3179.